TypeError:无法读取undefined的属性'length':ionic.bundle.min.js?

时间:2016-06-21 13:19:26

标签: javascript html angularjs json ionic-framework

我收到未捕获的TypeError:在我的离子应用程序的某个页面充值时,无法读取未定义错误的属性“长度”。

我想发送详细信息页面中所选广告的ID以列出此项目的所有详细信息

重新加载页面后出现错误。 the error

service.js

.factory('Annonces', function($http, $q) {
  // Might use a resource here that returns a JSON arra
    var dfd = $q.defer();
    var allannonces ;
      $http.get("http://localhost/json.php").then(function(response){
        annonces = response.data;
        console.log(annonces);
        dfd.resolve(annonces);   
        allannonces = annonces;
        console.log(allannonces);
        });
  return {
    all: function(){
       return dfd.promise;
      },
     get: function(annonceId) {
          for (var i = 0; i < allannonces.length; i++) {
        if (allannonces[i].id === parseInt(annonceId)) {
          return allannonces[i];
          console.log(allannonces[i]);
        }
      }
      return null;
    }
  };
  
});

controller.js

angular.module('starter.controllers', [])


.controller('AnnoncesCtrl', function($scope, allannonces) {
  
   $scope.annonces = allannonces;

})

.controller('AnnonceDetailCtrl', function($scope, $stateParams, Annonces) {
  $scope.annonce = Annonces.get($stateParams.annonceId);
  console.log($scope.annonce);
})

app.js

.config(function($stateProvider, $urlRouterProvider) {

  // Ionic uses AngularUI Router which uses the concept of states
  // Learn more here: https://github.com/angular-ui/ui-router
  // Set up the various states which the app can be in.
  // Each state's controller can be found in controllers.js
  $stateProvider


.state('annonces', {
      url: '/annonces',
  
          templateUrl: 'templates/annonces.html',
          controller: 'AnnoncesCtrl',
          resolve: {
        allannonces: function(Annonces) {
          return Annonces.all(); }
      }
        }
    )

.state('details', {
      url: '/annonces/:annonceId',
          templateUrl: 'templates/details.html',
          controller: 'AnnonceDetailCtrl'

        }
      
    )
  
   $urlRouterProvider.otherwise('/annonces');

});

0 个答案:

没有答案