在指令的链接中运行控制器中的功能

时间:2017-03-24 18:17:29

标签: javascript angularjs

我有一个指令:

.directive('directive', [ function () {
  return {
    restrict: 'AE',
    scope: {
        value: '=value'
    },
    link: function(scope, elem) {
        $document.bind('click', function() {
            ...call clickDocument() function
        });
    },
    bindToController: true,
    templateUrl: 'item.html',
    controller: 'Item as item'
  };
}])

该指令的控制器:

.controller('Item', [function Item () {

   this.clickDocument() = function() {
      console.log('click the document')
   }
}])

我想在文档点击事件中从控制器运行clickDocument()

我该怎么做?

谢谢。

1 个答案:

答案 0 :(得分:0)

您可以将控制器包含在链接注射中。

link: function(scope, elem, attrs, ctrl) {}