简体   繁体   中英

iterating through an array in a controller javascript

I am making a request to my database and that is returning it fine but I am wanting to store it into an array so that i can iterate through it again in another request. However, I can't do it as I cannot access the array length in my code.

'use strict'
var myApp = angular.module('dashApp', []);

var availiable = [];

myApp.controller('dashCtrl', ['$scope', '$http', function($scope, $http) {

    var getavailable = function() {
      $http.get('/availableList').success(function(response) {
          for (var i = 0; i < response.length; i++) {
            var av = new Object();
            av.Building = response[i].BuildingName;
            av.Townhall = response[i].Townhall;
            availiable.push(av);
          }
        };

        var main = function(req, response) {
          getavailable();
          console.log(availiable);
          console.log(avInt);
          for (var i = 0; i < availiable.length; i++) {
            console.log("in forloop");
            var count = getCount(availiable[i]);

          }
          $scope.BuilingList = $scope.finalList;
          $scope.finalList = "";

        };

        main();

      }]);

There is no need to even use the for loop

This should work fine.

 $http.get('/availableList').success(function(response) {
    available = response.data;
   ....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM