有人可以指出我在這裏可能做錯了什麼嗎?我有一個控制器使用$ http服務從我的服務器上的JSON文件中提取數據,並通過一個屬性傳遞給指令。問題是,即使我只看到4個對象在我的JSON循環中,它給了我325.更多的屬性是我無法訪問的。ng-repeat不準確地拉動數據
我的JSON
[{
"name": "Cute Shirt",
"Type": "Shirt",
"Size": "S,M,L,XL",
"Color": "R,G,B",
"SRC": "img/shirt.png"
}
,
{
"name": "Cute Shirt",
"Type": "Shirt",
"Size": "S,M,L,XL",
"Color": "R,G,B",
"SRC": "img/shirt.png"
}
,
{
"name": "Cute Shirt",
"Type": "Shirt",
"Size": "S,M,L,XL",
"Color": "R,G,B",
"SRC": "img/shirt.png"
}
,
{
"name": "Cute Shirt",
"Type": "Shirt",
"Size": "S,M,L,XL",
"Color": "R,G,B",
"SRC": "img/shirt.png"
}
]
我的控制器
"use strict";
function itemControl ($http,$scope) {
$http.get('doc/products.json').success(function(prodata){$scope.data = prodata;});
}
我的指令
app.directive("showcase", function() {
return {
restrict: "A",
template: '{{stuff.length}}',
scope: {
stuff: "@"
}
};
});
終於HTML
<div ng-controller="itemControl">
<div showcase stuff="{{data}}"></div>
</div>
嗯,是有可能,你的JSON是回來爲文本?這將解釋您的對象中的項目數量。 – Xesued
看起來確實如此。我該如何改變它,以便將實際的json轉換爲指令而不僅僅是文本? – richbai90