我試圖通過傳遞控制器來擴展指令。我可以通過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