AngularJS - 无法总结Scope的属性

时间:2014-07-01 16:32:29

标签: javascript angularjs

我在角度控制器中有以下角度代码:

代码:

$scope.orangeCrate.italian = {
    "description": "Annual",
    "payer": "farmer",
    "farmerCost": 0,
    "ownerCost": 21
};

$scope.orangeCrate.portugal = {
    "description": "Annual",
    "payer": "owner",
    "farmerCost": 6.5,
    "ownerCost": 0
};

$scope.orangeCrate.total = {
    "farmerCost": $scope.orangeCrate.italian.farmerCost + $scope.orangeCrate.portugal.farmerCost,
    "ownerCost": $scope.orangeCrate.italian.ownerCost + $scope.orangeCrate.portugal.ownerCost,
};

当我尝试将$scope.orangeCrate.total.farmerCost内的值打印到我的视图中时,我什么都没得到,即使我应该得到6.5

我做错了什么?

1 个答案:

答案 0 :(得分:1)

这样的事情会发生(对不起'#34;穷人"答案......我匆匆忙忙):

$scope.orangeCrate = {
    "italian": {
        "description": "Annual",
            "payer": "farmer",
            "farmerCost": 0,
            "ownerCost": 21
    },
        "portugal": {
        "description": "Annual",
            "payer": "owner",
            "farmerCost": 6.5,
            "ownerCost": 0
    }
};
$scope.orangeCrate.total = {
    "farmerCost": $scope.orangeCrate.italian.farmerCost + $scope.orangeCrate.portugal.farmerCost,
        "ownerCost": $scope.orangeCrate.italian.ownerCost + $scope.orangeCrate.portugal.ownerCost,
};