【问题标题】:ng-repeat not accurately pulling datang-repeat 不能准确地提取数据
【发布时间】: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


【解决方案1】:

来自 AngularJS 文档:

@ 或 @attr - 将本地范围属性绑定到 DOM 属性的值。结果始终是字符串,因为 DOM 属性是字符串。

使用 = 会有所帮助

= 或 =attr - 在本地范围属性和通过 attr 属性值定义的 name 的父范围属性之间设置双向绑定。

您需要将&lt;div showcase stuff="{{data}}"&gt;&lt;/div&gt; 更改为&lt;div showcase stuff="data"&gt;&lt;/div&gt;

【讨论】:

  • 谢谢,这正是我所需要的。我仍在学习 Angular 的所有细节,我不得不说文档有时可能有点难以理解。
【解决方案2】:

@ 更改为=

@ 表示它将对象插入到字符串中。

【讨论】:

    猜你喜欢
    • 2017-09-09
    • 2016-05-13
    • 2016-07-21
    • 1970-01-01
    • 2015-01-16
    • 1970-01-01
    • 2015-06-20
    • 2015-08-30
    • 1970-01-01
    相关资源
    最近更新 更多