有人能指出我可能做错了什么吗?我有一个控制器使用$http服务从服务器上的JSON文件中提取数据,并通过一个属性传递给一个指令。问题是,即使我只看到4个对象在我的JSON循环它给我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>
来自AngularJS文档:
@或@attr -将局部作用域属性绑定到DOM属性的值。结果总是字符串,因为DOM属性是字符串。
使用=会有帮助
= or =attr -在本地作用域属性和通过attr属性的值定义的name的父作用域属性之间建立双向绑定。
您想要将<div showcase stuff="{{data}}"></div>
更改为<div showcase stuff="data"></div>
将@
更改为=
@表示将对象插入到字符串中。