Answer the question
In order to leave comments, you need to log in
How to get the controller in which it is wrapped in a directive?
At first I started asking a more general question, but then I realized that it was caused by ignorance of how to get the controller in which the directive is wrapped.
<div ng-controller="SomeControeller as someControeller">
<some ng-click="someControeller.run()">####</some>
</div>
function SomeController(){
this.run = function(){
console.log('SomeController');
}
}
function someDirective(){
return {
controller: 'SomeControeller',
link: function($scope, iElm, iAttrs, controller) {
console.log(controller);
controller.run = function(){
console.log('directive');
}
}
};
}
var app = angular.module('app', []);
app.controller('SomeControeller', [SomeController]);
app.directive('some', [someDirective]);
angular.bootstrap(document, ['app']);
Answer the question
In order to leave comments, you need to log in
or if it is not necessary to receive it according to ideology, then why?
function myNgClick() {
return {
restrict: 'A',
scope: {
callback: "&myNgClick"
},
link: function (scope, el) {
el.bind('click', function (e) {
scope.$apply(function () {
scope.callback({$event: e});
});
});
}
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question