T
T
The Whiz2014-11-15 17:00:56
JavaScript
The Whiz, 2014-11-15 17:00:56

Why doesn't Angular count objects?

I am getting objects from a factory, the factory is pulling them from Firebase.
This is what the controller looks like (partially):

$scope.customers = [];

function getTotal() {
  $scope.custTotal = $scope.customers.length;
}

function init() {
  $scope.customers = customersFactory.getCustomers();
  getTotal();
}

init();

In the {{custTotal}}
view, the problem is that $scope.customers.length doesn't return anything at all, while $scope.customers returns an array:
[{"city":"Rutten","id":1,"joined":"2011-05-02","name":"Tanner","orders":[{"id":2,"product":"Left shoe","total":2.95}],"$id":"0","$priority":null},{"city":"San Francisco","id":2,"joined":"2011-12-12","name":"Joffrey","orders":[{"id":1,"product":"Right shoe","total":4.95},{"id":2,"product":"Left shoe","total":9.95}],"$id":"1","$priority":null}]

If you put {{customers.length}} in the view, everything works. Why doesn't length work inside a function?
I thought that the problem was in the asynchronous loading, I tried using then and $watch, but nothing changes.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikita Gushchin, 2014-11-15
@iNikNik

It seems to me that you have a very strange decision. I would do

function getTotal() {
  return $scope.customers.length;
}

And in the view I used {{getTotal()}} . What you end up doing is calling getTotal every time the customers array changes.
+ getTotal logically - to get the amount of something, but it does not return anything for you.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question