无法读取属性长度angularjs

时间:2013-09-07 15:28:06

标签: angularjs yeoman

我在使用yeoman生成的角度应用程序中有以下代码

'use strict';

function CoffeeListCtrl($scope, $http) {
  $http.get('data/coffee.json').success(function(data) {
    $scope.coffees = data;
  });
    $scope.orderProp = 'name';
    $scope.orderType = '';

    $scope.goTo = function (path) {
        $location.path(path);
    }

$scope.showNresults = function (n) {
        if (n==-1) {
            return $scope.coffees.length;
        } else { 
            return n;
        }
        //REVIEW: code optimization .. shrink version
        // message = ('Stephen' === name) ? "Welcome back Stephen" : "Welcome " + name;
  }

 $scope.numberOfPages=function(itemsPerPage){
        if (itemsPerPage == -1) itemsPerPage=$scope.coffees.length;
    return Math.ceil($scope.coffees.length/itemsPerPage);              
  }
}

我知道为什么我会得到以下console output

更新 有以下html代码段:

<div class="row-fluid"> 
    <ul class="coffees">
      <li class="span4 coffeeitem" ng-repeat="coffee in (filteredItems = (coffees | filter:query | orderBy:orderProp | filter:orderType | startFrom: (currentPage*resultspp)-resultspp | limitTo: showNresults(resultspp)))">
        <a href="#/coffees/{{coffee.id}}">{{coffee.name}}</a>
        <p>{{coffee.basicInfo}}</p>
        <button class="coffeebutton" type="button">Add to list</button>
      </li>
      <p ng-show="(coffees | filter:query).length == 0" id="message">No coffee found, please refine your search</p>
    </ul>
  </div>

并且已经确定了导致循环依赖性错误startFrom: (currentPage*resultspp)-resultspp | limitTo: showNresults(resultspp)

的以下内容

1 个答案:

答案 0 :(得分:1)

您需要初始化$scope.coffees

将它放在CoffeeListCtrl

$scope.coffees = [];

$scope.numberOfPages文件加载完成之前,data/coffee.json正在进行评估。通过初始化coffees属性,可以在调用numberOfPages函数时确保该数组存在。