javascript - 将数组从 ng-repeat 项传递给指令

标签 javascript angularjs angularjs-directive angularjs-ng-repeat

我有以下指令:

var productsTable = function() {
    return {
        restrict: 'E',

        scope: {
            products: '@'
        },

        templateUrl: '../js/app/templates/products_table.html'
    };
};

模板是一个简单的表格:

<table class="table table-striped">
    <thead>
        <tr>
            <th>#</th>
            <th>Name</th>
            <th>Quantity</th>
        </tr>
    </thead>
    <tbody>
        <tr ng-repeat="product in products track by $index">
            <td><% product.id %></td>
            <td><% product.name %></td>
            <td><% product.quantity %></td>
        </tr>
    </tbody>
</table>

并且在这段代码中使用了它:

<div ng-controller="MainController">
    <div ng-repeat="entry in entries">
        <products-table products="entry.products"></products-table>
    </div>
</div>

entries 变量的一个例子是:

var entries = [
    {
        id: 1,
        products: [
            {
                id: 1001,
                name: 'Product 1',
                quantity: 30
            },
            {
                id: 1002,
                name: 'Product 2',
                quantity: 3
            }
        ]
    },
    {
        id: 2,
        products: [
            {
                id: 3001,
                name: 'Product 3',
                quantity: 450
            }
        ]
    }
];

问题是:指令不识别 products 数组,并且不显示表格。

最佳答案

试试这个

scope: {
   products: '='
},

同样在 Angular 中你应该使用 {{ }}而不是 <% %>

Example

关于javascript - 将数组从 ng-repeat 项传递给指令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33259566/

相关文章:

javascript - 立体图的插值

javascript - 将 ng-repeat 与指令一起使用会导致子指令不更新

angularjs - 使用 ng-animate $animate 自定义动画

javascript - Angularjs - 动态数据指令 - 从字符串创建对象

javascript - 只使用一次 Angular Directive(指令)

javascript - 为什么 ng-keypress 不会更改 ng-model 值

javascript - JSON 的二进制编码?

javascript - 使用 Google Closure Compiler Application 在一个文件中压缩所有文件 .js

javascript - getTimezoneOffset() 方法

javascript - 使用 Angular 玩游戏 (60 fps)