问题
This is my code:
app.js
$http.get("http://angularjs.uum.my/tt.json")
.success(function(response) {$scope.scrollItems = response;});
$http.get("http://angularjs.uum.my/tt2.json")
.success(function(response) {$scope.scrollItems2 = response;});
tt.json - have only 1 element
- http://angularjs.uum.my/app/#/timetable
tt2.json - have two 2 elements
- http://angularjs.uum.my/app/#/timetable2
The problem is with page no. 1, where only one element exist, the page rendered empty list repeating x number of times (where x equal to number of child elements)
Is this known bug? How to solve this problem if the number of elements is unknown beforehand to the developer?
Please help, thanks for your time.
回答1:
That is not a bug, your first json is
{"namakursus":"John", "pensyarah":"Doe", "yeh":"yeh"}
and the second one is
[
{"namakursus":"John", "pensyarah":"Doe", "yeh":"yeh"},
{"namakursus":"John2", "pensyarah":"Doe2", "yeh":"yeh2"}
]
this means the first one is not an array, so it is reapiting an object
if you want to get the same behavior your json should be and array with only 1 element, not an single object
[{"namakursus":"John", "pensyarah":"Doe", "yeh":"yeh"}]
来源:https://stackoverflow.com/questions/27085625/ng-repeat-one-item-only-source