为同一指令设置多个后缀(angularjs)

时间:2016-02-26 10:27:40

标签: angularjs angularjs-directive

假设我们有以下指令:

app.directive('testList', function () {
    return {
        restrict: 'A',
        link: function (scope, element, attrs) {
            //do stuff
        }
    }
});

我想从html

中的多个元素调用此指令
<button test-list-add></button>
 <table test-list="listObject">
    <tr ng-repeat="list in testList">
       <td test-list-click>list.item</td>
    </tr>
</table>

是否可以针对以linktest-listtest-list-addtest-list开头的每个属性进入test-list-click函数?

谢谢

1 个答案:

答案 0 :(得分:0)

您希望将属性传递给指令并执行某些操作,具体取决于属性:

app.directive('testList', function () {
    return {
        restrict: 'A',
        scope: {
           'add': '@', // Text
           'click': '&' // function
        },
        link: function (scope, element, attrs) {
            // scope.add - your Text you passed
            // scope.click(); - calls your function you passed
        }
    }
});

<button test-list add="test"></button>
 <table test-list="listObject">
    <tr ng-repeat="list in testList">
       <td test-list click="myFunc()">list.item</td>
    </tr>
</table>

你不能重复一个指令(你正在尝试做什么)。

如果要为指令创建多个后缀,则必须逐个声明它们:

app.directive('testListAdd', function () {...});
app.directive('testListclick', function () {...});

<test-list-add></test-list-add>
<test-listclick></test-listclick>