javascript - 带参数的 angularjs 指令绑定(bind)函数

标签 javascript angularjs angularjs-directive

我想将带参数的函数发送给 angularjs 指令。 因为该指令始终只使用一个自己的变量。 Controller 发送到指令的函数正在处理该变量。

我制作如下 Controller

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”参数的函数。

这是 View (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);
            }
        }
    }
});

工作正常,但控制台显示infdig错误

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: '=',
}

来自source

Best Practice: use &attr in the scope option when you want your directive to expose an API for binding to behaviors

其次:因为您要返回一个函数,所以需要第二对括号来调用内部函数,请参见下文:

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

请参阅工作示例 here

完整代码:

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

关于javascript - 带参数的 angularjs 指令绑定(bind)函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36174985/

相关文章:

javascript - 返回子元素的链接事件监听器

javascript - ionic ionic 导航按钮 ng-如果第一次加载不起作用

angularjs - "Error: type property can' t be changed"。使用 ngTagInput 库 (AngularJs) 时出错

php - 通过 open.window 传递 php 变量

javascript - 检查 toString 是否已分配

javascript - 使用 Passport JS 的自定义策略 Oauth 身份验证

javascript - Angular Directive(指令)范围不绑定(bind)数据

javascript - 删除 Angular 文本中的空白

angularjs-directive - 带有从外部调用的方法的 AngularJS 指令

javascript - 我应该如何实现 Angular 选择指令?