Answer the question
In order to leave comments, you need to log in
How to get the controller's this from a directive?
app.directive("name", function () {
controller : function () {
},
link:function(scope,elem,attr,contr){
// от сюда
// взять this котроллера . например selfContr
selfContr.method = fucntion () {
return elem
}
}
}
app.directive("name2", function () {
require : "name",
controller : function () {
//this
},
link:function(scope,elem,attr,contr){
contr.method();
}
}
Answer the question
In order to leave comments, you need to log in
See how useful it is to read off-line documentation
https://docs.angularjs.org/guide/directive#creatin...
Directly your code!
DEMO
// Code goes here
angular.module('app', [])
.controller('FooCtrl', FooController)
.controller('BarCtrl', BarController)
.directive('foo', fooDirective)
.directive('bar', barDirective);
FooController.$inject = ['$window'];
function FooController($window) {
this.notify = function() {
$window.alert('foo ctrl');
};
}
FooController.$inject = ['$window'];
function BarController($window) {
this.notify = function() {
$window.alert('bar ctrl');
};
}
function fooDirective() {
return {
controller: 'FooCtrl'
};
}
function barDirective() {
return {
controller: 'BarCtrl',
controllerAs: 'ctrl',
require: 'foo',
link: function(scope, elem, attrs, ctrl) {
if (elem[0].tagName === 'A') {
elem.on('click', function() {
ctrl.notify();
});
} else {
elem.on('click', function() {
scope.ctrl.notify();
});
}
}
};
}
this is how you can
app.directive("name", function () {
controller : function ($scope) {
var self = this;
$scope.toThis = function (name , data ) {
self[name] = data
}
},
link:function(scope,elem,attr,contr){
scope.toThis("slide", elem)
}
}
app.directive("name2", function () {
require : "name",
controller : function () {
//this
},
link:function(scope,elem,attr,contr){
contr.slide
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question