Answer the question
In order to leave comments, you need to log in
How to make code work for Angular promise?
Have a service
crmServices.factory('Insurances', ['$resource',
function ($resource) {
return $resource('api/public/insurances/:id', {}, {
query: {method: 'GET', isArray: true, cache: true},
get: {method: 'GET', isArray: false},
save: {method: 'POST'},
update: {method: 'PUT'},
delete: {method: 'DELETE'}
});
}]);
$scope.insurances = Insurances.query()
$scope.getLen = function() {
return = $scope.insurances.length
}
TypeError: Cannot read property 'length' of undefined
Answer the question
In order to leave comments, you need to log in
You would write how the insurances in the osprey appear.
It may be enough $scope.insurances = [];, it may be worth moving the data acquisition into the resolve section. Lots of options, all have their pros and cons.
$scope.getLen = function() {
$scope.insurances.query().then(function(data) {
$scope.length = data.length
});
}
In your case, it's enough just to check the promise for resolveved:
$scope.getLen = function() {
if(!$scope.insurances.$resolved)
return 0;
return $scope.insurances.length;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question