asp.net web api - AngularJS - get length of array from a webApi call on first call only -
i trying display text 'showing x of y attractions' x number showing , y total number stored in database.
i can x using {{attractions.length}}
- i'm stuck on best way y?
currently obtaining data through service call, doing $http
web api controller of data initially, can filtered.
$scope.attractions = dataservice.getallattractions();
i like:
$scope.totalattractions = dataservice.getallattractions().length;
so question best way totalattractions
in scenario?
edit, getallattractions
dataservice function:
function getallattractions() { $http.get("/api/v1/attractions") .then(function (response) { // success angular.copy(response.data, attractions); }, function () { // failure }); return attractions; }
you trying return undefined attractions
getallattractions()
.
$http.get
service asynchronous , returns promise. should return promise instead of attractions
.
in current code, i've inserted log display on console.
function getallattractions() { $http.get("/api/v1/attractions") .then(function (response) { // success console.log("success"); angular.copy(response.data, attractions); }, function () { // failure console.log("fail"); }); console.log("returned value"); return attractions; }
assuming call webservice successfully, console display:
returned value success
that because success callback executed after return attractions
.
you should instead:
dataservice
function getallattractions() { return $http.get("/api/v1/attractions"); }
controller
dataservice.getallattractions() .then(function (response) { // success $scope.attractions = response.data; $scope.totalattractions = $scope.attractions.length; }, function () { // failure });
Comments
Post a Comment