在AngularJS中从父控制器调用指令的方法
- 论坛
- 在AngularJS中从父控制器调用指令的方法
13 浏览
在AngularJS中从父控制器调用指令的方法
我正在使用AngularJS的别名控制器模式。我无法从父控制器中访问(或者不知道如何访问)指令方法。
我在控制器中有一个函数,应该调用一个指令方法,但是这个指令方法在this
控制器值中不可用。
以下是我的代码。我做错了什么?
JS
angular.module('myApp', []). controller('MyCtrl', function(){ this.text = '控制器文本'; this.dirText = '指令文本'; this.click = function(){ this.changeText(); } }) .directive('myDir', function(){ return { restrict: 'E', scope: { text: '=' }, link: function(scope, element, attrs){ scope.changeText = function(){ scope.text = '新的指令文本'; }; }, template: '{{text}}
' }; });
HTML
{{ctrl.text}}
这里是一个包含此代码的codepen链接。