第一次尝试获取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