来自Directive AngularJS的调用函数

时间:2016-01-22 09:26:14

标签: javascript html angularjs

我试图在指令中调用一个函数。这是一个html:

http://

这是指令:

<span class="delete-link">
    <delete></delete>
    <input type="button" data-ng-click="removeRow(task)"/>
</span>

这是我使用的例子:

http://jsfiddle.net/mrajcok/T96Zu/

但它不会删除元素。我错过了什么?

2 个答案:

答案 0 :(得分:2)

.directive('delete', function() {

      return {

            restrict: 'E',
            replace: true,
            template: '<div></div>',

            link: function(scope ,element) {

                    scope.removeRow = function (task) {
                        scope.tasks.splice(scope.tasks.indexOf(task), 1);
                    }

            }

        }

    });

答案 1 :(得分:1)

使用范围代替$ scope

.directive('delete', function() {
    return {
        restrict: 'E',
        replace: true,
        template: '<div></div>',
        link: function(scope, element) {
            element.click(function(){
                scope.removeRow = function (task) {
                    scope.tasks.splice(scope.tasks.indexOf(task), 1);
                }
            });
        }
    }
});