我不明白如何在我的控制器中将 data.properties 分配给 $scope 之前等待资源调用完成。
这是我的困惑:
我有一个返回此的资源调用:
[
{
"PreAlertInventory": "5.000000",
"SharesInInventory": "3.000000",
"TotalSharesSold": "2.000000",
"TotalMoneySharesSold": "18.000000",
"TotalSharesBought": "0.000000",
"TotalShareCost": "0.000000",
"EstimatedLosses": "0.000000"
}
]
这是控制器的适用代码:
$scope.alertSwap = function () {
var data = Data.query(); // gets the data. works as expected
//$scope.test = data; //works as expected. can access data and properties in template
$scope.test = data.TotalMoneySharesSold; //does not work. is empty???
}
我可以从模板访问数组及其属性,但不能从控制器内部访问。
如何从调用资源的控制器内部访问子元素?
http://plnkr.co/edit/JnLqq4v7lKlYOHg85Jma?p=preview
回答:感谢所有帮助过的人:
我需要为资源调用分配回调,并从函数内分配元素:
$scope.data = Data.query(myParams, function(data) {
$scope.test = data[0].TotalSharesBought;
});