我有一個問題,我確定有一個簡單的解決方案,但我找不到它。我有一個JSON文件,裏面有三個獨立的數組。網站1,網站2和網站3。我通過一個app.controller檢索,並需要使用ng-repeat在3個獨立的選擇列表中生成選項。我沒有創建3個app.controller實例,而是使用3個數組在表單級別創建一個實例,然後使用ng-repeat 3次來獲取每次相關的值,但我無法解決如何使用ng-repeat檢索值。該JSON我有如下:ng-repeat在JSON文件中的兩個獨立數組Angular JS
[
{
"Websites1": [
{
"0": "1",
"ID": "1",
"1": "Value 1",
"WebsiteName": "Value 1",
"2": "EI",
"WebsiteCode": "EI"
},
{
"0": "2",
"ID": "2",
"1": "Value 2",
"WebsiteName": "Value 2",
"2": "IC",
"WebsiteCode": "IC"
},
{
"0": "3",
"ID": "3",
"1": "Value 3",
"WebsiteName": "Value 3",
"2": "SO",
"WebsiteCode": "SO"
}
]
},
{
"Websites2": [
{
"0": "1",
"ID": "1",
"1": "Value 1a",
"WebsiteName": "Value 1a",
"2": "EI",
"WebsiteCode": "EI"
},
{
"0": "2",
"ID": "2",
"1": "Value 2a",
"WebsiteName": "Value 2a",
"2": "IC",
"WebsiteCode": "IC"
},
{
"0": "3",
"ID": "3",
"1": "Value 3a",
"WebsiteName": "Value 3a",
"2": "SO",
"WebsiteCode": "SO"
}
]
},
{
"Websites3": [
{
"0": "1",
"ID": "1",
"1": "Value 1a",
"WebsiteName": "Value 1a",
"2": "EI",
"WebsiteCode": "EI"
},
{
"0": "2",
"ID": "2",
"1": "Value 2a",
"WebsiteName": "Value 2a",
"2": "IC",
"WebsiteCode": "IC"
},
{
"0": "3",
"ID": "3",
"1": "Value 3a",
"WebsiteName": "Value 3a",
"2": "SO",
"WebsiteCode": "SO"
}
]
}
]
而且控制器:
app.controller('FormController', [ '$http', function($http) {
var form = this;
form.json = [];
var config = {
params: {
command: 'banner.json.form.website',
ajax: 'true'
}
}
$http.get('https://api.myjson.com/bins/o9r47', config).success(function(data) {
console.log(data);
form.json = data;
}).error(function(data) {
console.log('error');
});
}]);
都好那裏,被正確返回JSON。這裏的NG-重複:
<div ng-controller="FormController as form" class="portlet-body form">
<select class="form-control select2">
<option ng-repeat="json in form.json.Websites" value="{{json.WebsiteCode}}">{{json.WebsiteName}}</option>
</select>
</div>
如果我使用一個JSON對象,只有頂層數據,那麼這個工程:
<div ng-controller="FormController as form" class="portlet-body form">
<select class="form-control select2">
<option ng-repeat="json in form.json" value="{{json.WebsiteCode}}">{{json.WebsiteName}}</option>
</select>
</div>
但我無法弄清楚如何從一個陣列拉取數據在JSON對象內。有人可以幫忙嗎?
謝謝!
請確認您收到數據包裝成單個數組元素,所以'['&']'周圍的例子括號是不一個失誤。 – Seprum