我知道如何在子指令的link
函数中获取父级的directive
控制器。
但是,我宁愿避免使用link
函数(和$scope
全部),并将所有代码都放在指令的controller
函数下。Angular - 在子指令的控制器中获取父指令的控制器(不是链接功能)
angular.directive('parent', function(){
return {
templateUrl: '/parent.html',
scope: true,
bindToController: true,
controllerAs: 'parentCtrl',
controller: function(){
this.coolFunction = function(){
console.log('cool');
}
}
}
});
angular.directive('child', function(){
return {
templateUrl: '/child.html',
require: '^parent',
scope: true,
bindToController: true,
controllerAs: 'childCtrl',
controller: function() {
// I want to run coolFunction here.
// How do I do it?
}
}
});
任何帮助表示赞赏!
ü要避免使用链接?为什么? –
我想用Angular2友好的方式编写代码。 – Dmitry
@Dmitry坚持控制器和jqlite(正如答案正确表明的那样)会让你的代码对Angular 2产生敌意(这与友好性相反)。 – estus