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

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -