2014-04-07 4 views
1

위치 이름별로 재고가 얼마나 있는지 세고 있습니다. 모든 작동하지만, location name = null 경우 난 내 콘솔에서 오류가 발생할 수 Cannot read property 'name' of nullAngularJS의 null 값 오류

$scope.stored = function(name) { 
     $scope.count = 0; 
     for(var i = 0; i < $scope.inventories.length; i++){ 
      if($scope.inventories[i].location.name == name){ 
       $scope.count = $scope.count + 1; 
      } 
     } 
      return $scope.count; 
     }; 
+1

을 변경해야 if ($ scope.inventories [i] .location.name == name) {'to'if ($ scope.inventories [i] .location && $ scope.inventories [i] .location.name == name) { ' –

+0

중복 가능 [null 길이의 속성을 읽을 수 없습니다 (자바 스크립트)] (http://stackoverflow.com/questions/15731559/c) annot-read-property-length-of-null-javascript) –

답변

2

내가 코멘트에서 답을 끌어와 미래의 방문자 여기를 게시 오전 :

당신은 if($scope.inventories[i].location.name == name){if($scope.inventories[i].location && $scope.inventories[i].location.name == name){