AngularJS中的空值错误

AngularJS中的空值错误,angularjs,Angularjs,我正在按地点名称计算我有多少存货。一切正常,但如果location name=null我的控制台中出现错误无法读取null的属性“name” $scope.stored = function(name) { $scope.count = 0; for(var i = 0; i < $scope.inventories.length; i++){ if($scope.inventories[i].location.name == nam

我正在按地点名称计算我有多少存货。一切正常,但如果
location name=null
我的控制台中出现错误
无法读取null的属性“name”

$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;
        };
$scope.stored=函数(名称){
$scope.count=0;
对于(变量i=0;i<$scope.inventory.length;i++){
如果($scope.inventory[i].location.name==name){
$scope.count=$scope.count+1;
} 
}
返回$scope.count;
};

我将从评论中找到答案,并在这里发布给未来的访问者:

您需要将
if($scope.inventory[i].location.name==name){
更改为
if($scope.inventory[i].location&&$scope.inventory[i].location.name==name){
更改为
if($scope.inventory[i].location&$scope.inventory[i].location==name][i].location==name){
可能重复的