簡體   English   中英

指令AngularJS的調用函數

[英]Call function from Directive AngularJS

我正在嘗試在指令內調用函數。 這是一個html:

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

這是指令:

.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);
                    }
                });
            }
        }
    });

這是我使用的示例:

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

但是它不會刪除元素。 我想念的是什么?

.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);
                    }

            }

        }

    });

使用范圍而不是$ 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);
                }
            });
        }
    }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM