我试图通过传递控制器来扩展指令。我可以通过 获取父指令的控制器require
,但我也想在扩展控制器上定义一个控制器。
.directive('smelly', function(){
return {
restrict: 'E',
controller: function(){
this.doWork = function(){ alert('smelly work'); };
},
link: function($scope, $element, $attributes, controller){
$element.bind('click',function(){
controller.doWork();
});
}
};
})
.directive('xtSmelly', function(){
return {
controller: function(){
this.name = "brian";
},
require: 'smelly',
link: function($scope, $element, $attributes, smellyController){
smellyController.doWork = function(){
alert('xt-smelly work by: ' + xtSmellyController.name);
};
}
};
})
HTML
<smelly xt-smelly>click me</smelly>
如何访问 xtSmellyController.name?