Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript/AngularJS-在初始化期间访问对象的属性_Javascript_Angularjs - Fatal编程技术网

Javascript/AngularJS-在初始化期间访问对象的属性

Javascript/AngularJS-在初始化期间访问对象的属性,javascript,angularjs,Javascript,Angularjs,我想访问total属性,如果total大于1,则将名词属性更改为“items”。并使用total和noun属性更新句子属性 这是我的密码: $scope.itemCounter = { total: 0, noun: $scope.itemCounter.total <= 1 ? "item" : "items", sentence: $scope.itemCounter.total+" "+$scope.itemCo

我想访问total属性,如果total大于1,则将名词属性更改为“items”。并使用total和noun属性更新句子属性

这是我的密码:

$scope.itemCounter = {
            total: 0,
            noun: $scope.itemCounter.total <= 1 ? "item" : "items",
            sentence: $scope.itemCounter.total+" "+$scope.itemCounter.noun+" remaining"
        };
预期输出应为:

Cannot read property 'total' of undefined
$scope.itemCounter.sentence = "0 item remaining";
是因为$scope.itemCounter尚未初始化吗?如果这个方法不可行,有谁能给我提供更好的方法吗?

您可以这样做:

$scope.itemCounter = {
            total: 0,
            noun: $scope.itemCounter.total <= 1 ? "item" : "items",
            get sentence(){ 
           return $scope.itemCounter.total+" "+$scope.itemCounter.noun+" remaining"
         }
  };
$scope.itemCounter={
总数:0,
名词:$scope.itemCounter.total