main.js
var myFarm = $http.get('farmList.JSON').success(function(data) {
$scope.farms= data;
});
var myFarm = $ scope.farms;
var updateFarms = function(){
for (i = 1; i <= myFarm.length; i++)
{
// code to update farms
var sFarm = myFarm[i - 1];
//loop thru myFarm and update it
$scope.farms = myFarm; //assign updated farms back to farms
}
}
farmList.json
[
{"farmName": "A", "type": "B"},
{"farmName": "C", "type": "D"}
]
我希望能够将json变成一个变量,以便我可以保留副本。如果农场更新,我应该能够将myFarm更新为新的。我如何使用像http.get?
这样的异步函数来处理变量答案 0 :(得分:1)
$http.get
返回一个承诺。您必须评估then()
并获取结果并分配给您的范围变量。
http.get('farmList.JSON').then(function(data) {
$scope.farms = data;
});
答案 1 :(得分:0)
将$scope.farms
用于所有内容:
$scope.farms = [];
$http.get('farmList.JSON').success(function(data) {
$scope.farms= data;
});
var updateFarms = function() {
for (i = 1; i <= $scope.farms.length; i++)
{
// code to update farms
var sFarm = myFarm[i - 1];
//loop thru myFarm and update it
}
};