Answer the question
In order to leave comments, you need to log in
How to use factory data in Angular?
Greetings.
have the following code inside the controller
$scope.billings = Billing.query();
$scope.getTotalAmount = function(index){
//тут посложнее логика
return $scope.billings[index].bills;
}
Can't interpolate:
{{getTotalAmount()}}
TypeError: Cannot read property 'bills' of undefined
Answer the question
In order to leave comments, you need to log in
If you use promises, then you can do this:
$scope.getTotalAmount = function(index){
//тут посложнее логика
if(!$scope.billings.$resolved)
return 0;
return $scope.billings[index].bills;
}
$scope.getTotalAmount = function(index){
//тут посложнее логика
if(!$scope.billings[index])
return 0;
return $scope.billings[index].bills;
}
You have getTotalAmount function(index)
Let me explain. getTotalAmount , when called, expects you to pass it an index by which it can return $scope.billings[index].bills to you. If you do not pass index to the view by simply calling {{getTotalAmount}} instead of {{getTotalAmount(2 or any index by which you can get this is your $scope.billings[index].bills)}} you will naturally get an error, since index in your case undefined, and $scope.billings[undefined] God himself ordered to be undefined for such inputs. Well, js in general, and angular in particular, cannot guess what kind of data you expect from external approximate signs.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question