繁体   English   中英

带参数的angularjs指令绑定函数

[英]angularjs directive binding function with argument

我想将带有参数的函数发送给angularjs指令。 因为此指令始终仅使用一个自己的变量。 控制器发送给指令的功能就是使用该变量。

我制作如下控制器

app.controller('TestController', function($scope) {
    $scope.args = ['arg1', 'arg2', 'arg3', 'arg4', 'arg5'];
    $scope.makeDirectiveFunction = makeDirectiveFunction;

    function iWantDoThisInDirective(arg, content) {
        alert(arg + content);
    }

    // Controller make function with 'arg'
    // and return function that have 'content' parameter
    // finally doing controller function with 'arg' and 'content' argument
    function makeDirectiveFunction(arg) {
        return function editorFunc(content) {
            iWantDoThisInDirective(arg, content);
        }
    }
});

我想用'arg'和'content'参数执行'itIsControllerFunction'。 所以我创建了一个'makeDirectiveFunction'函数来生成具有'content'参数的函数。

这是视图(index.html)

<div ng-repeat="arg in args">
    <redactor do-func="makeDirectiveFunction(arg)"></redactor>
</div>

这是指令

app.directive('redactor', function($timeout) {
    return {
        restrict: 'EA',
        scope: {
            doFunc: '=',
        },
        template: '<button ng-click="doInDirective()">Register!</button>',
        link: function(scope, element, attrs) {
            scope.doInDirective = function() {
                var content = "I'm in directive!";
                scope.doFunc(content);
            }
        }
    }
});

它工作正常,但控制台显示输入错误

Error: $rootScope:infdig
Infinite $digest Loop
10 $digest() iterations reached. Aborting!
Watchers fired in the last 5 iterations: [[{"msg":"fn: function (c,e,f,g){f=d&&g?g[0]:a(c,e,f,g);return b(f,c,e)}"}],[{"msg":"fn: function (c,e,f,g){f=d&&g?g[0]:a(c,e,f,g);return b(f,c,e)}"}],[{"msg":"fn: function (c,e,f,g){f=d&&g?g[0]:a(c,e,f,g);return b(f,c,e)}"}],[{"msg":"fn: function (c,e,f,g){f=d&&g?g[0]:a(c,e,f,g);return b(f,c,e)}"}],[{"msg":"fn: function (c,e,f,g){f=d&&g?g[0]:a(c,e,f,g);return b(f,c,e)}"}]]

也许它导致嵌套返回功能,但我无法解决它。 我希望你能帮忙。 谢谢。

首先,当您将函数传递给指令时使用

scope: {
    doFunc: '&',
}

代替

scope: {
    doFunc: '=',
}

来自消息来源

最佳实践:当您希望指令公开API以绑定行为时,请在范围选项中使用&attr

其次:因为你要返回一个函数你需要第二对括号来调用内部函数,请看下面:

scope.doInDirective = function() {
    var content = "I'm in directive!";
    scope.doFunc()(content);
}

请在这里查看工作示例

完整代码:

<div ng-controller="TestController as vm">
  <div ng-repeat="arg in args">
    <redactor do-func="makeDirectiveFunction(arg)"></redactor>
  </div>
</div>

JS:

var myApp = angular.module('application', []);

myApp.controller('TestController', ['$scope', function($scope) {
  $scope.args = ['arg1', 'arg2', 'arg3', 'arg4', 'arg5'];
  $scope.makeDirectiveFunction = makeDirectiveFunction;

  function iWantDoThisInDirective(arg, content) {
    alert(arg + content);
  }

  // Controller make function with 'arg'
  // and return function that have 'content' parameter
  // finally doing controller function with 'arg' and 'content' argument
  function makeDirectiveFunction(arg) {
    return function editorFunc(content) {
      iWantDoThisInDirective(arg, content);
    }
  }
}]).directive('redactor', function($timeout) {
  return {
    restrict: 'EA',
    scope: {
      doFunc: '&doFunc',
    },
    template: '<button ng-click="doInDirective()">Register!</button>',
    link: function(scope, element, attrs) {
      scope.doInDirective = function() {
        var content = "I'm in directive!";
        scope.doFunc()(content);
      }
    }
  }
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM