我试图通过传递控制器来扩展指令。我可以通过require
获取父指令的控制器,但我也想在扩展控制器上定义一个控制器。Angularjs扩展指令控制器
.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?
嗯它有效,但它是正确的方式吗?它是否通过将问题抛入范围来破坏分离? – user2809175