我正在尝试学习如何使用 Angular 加载 JSON。我是两个主题的初学者,并且坚持一些真正的初学者主题。
我有一个名为 food.json 的 JSON 文件。它有这样的结构:
[
{"uid":"55","title":"Nussbrot","code":"U VP 0000030","unit":[{"title":""}],"unitgram":"0","unitgram_second":"0","sorting":"0"},
{"uid":"58","title":"Walnu\u00dfbrot","code":"X 39 2000002","unit":[{"title":"Scheiben"}],"unitgram":"45","unitgram_second":"0","sorting":"0"}
]
按照http://toddmotto.com/ultimate-guide-to-learning-angular-js-in-one-day/的说明,我有一个这样的控制器:
myApp.controller('EmailsCtrl', ['$scope', function ($scope) {
$scope.foodlist = {};
$scope.foodlist.foodtitle = '';
$http({
method: 'GET',
url: 'food.json'
})
.success(function (data, status, headers, config) {
$scope.foodlist = data.; //I don't know what to call here as my data elements have no names?
})
.error(function (data, status, headers, config) {
//error
});
}]);
我坚持的第一点是,如果 JSON 对象没有名称,我如何将数据分配给我的“$scope.foodlist”?
到目前为止,我已经为我的无效尝试构建了一个 Plunkr,并且希望在 a) 读取 JSON 和 b) 将 JSON 数据分配给控制器中的值方面提供任何指导。