第一次嘗試獲取json數據並使用AngularJS將其綁定到我的表單。我能夠以這種方式綁定json,但它需要我手動將「data」匹配到我的html中的每個項目。從我的控制器AngularJS http get json
HTTP調用:
$http.get('static.json').
success(function(data, status, headers, config) {
// here I have to manually copy all my json to bind with "data"
$scope.SiteID = data.SiteID;
$scope.SiteCode = data.SiteCode;
}).
error(function(data, status, headers, config) {
// log error
});
我的JSON:
{
"SiteID":"mySiteIDGoesHere",
"SiteCode":"mySiteCodeGoesHere"
}
是否有我的JSON自動綁定,無需手動完成每個項目的方法嗎?例如:
$http.get('static.json').
success(function(data, status, headers, config) {
"just pull in whatever my json is and bind it"
$scope.WhateverIsInJSONID = data.WhateverIsInJSONID;
$scope.WhateverIsInJSONCode = data.WhateverIsInJSONCode;
}).
error(function(data, status, headers, config) {
// log error
});
只需設置'$ scope.something = data',然後你的視圖就會像:'{{something.WhateverIsInJSONID}}',或者其他任何 – Tom