【发布时间】:2013-04-17 03:07:47
【问题描述】:
有人可以向我指出我在这里做错了什么吗?我有一个控制器使用 $http 服务从服务器上的 JSON 文件中提取数据,并将其通过属性传递给指令。问题是,即使我在 JSON 循环中只看到 4 个对象,它给了我 325。此外,我无法访问任何属性。
我的 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 是否有可能以文本形式返回?这将解释您的对象中的项目数量。
-
看起来确实如此。如何更改它以便将实际的 json 传输到指令而不仅仅是文本?
标签: json angularjs angularjs-directive