I'm trying to get two directives to communicate with each other via their (inner defined) controllers, but I'm new to angular and still not clear on a couple of things.
Essentially, I just want two separate directives where, when you click on an element inside directive A, some function within B executes; I want to tell B to update itself, based on what happens in A. What I've got so far:
app.directive('A', function(){
return{
restrict: 'E',
require:'B',
templateUrl:'a.html',
link: function(scope, element, attr, bController){
//should I run B's behavior here?
//how? can't reach B's controller
},
controller: function(){
//or here?
//how do I access B's controller here?
},
controllerAs:'A'
};
});
app.directive('B', function(){
return{
restrict: 'E',
templateUrl:'b.html',
link: function(scope, element, attr){
},
controller: function(){
//here there'll be functions that manipulate
//elements inside this directive (B)
},
controllerAs:'B'
};
});
I'm getting an error, since A is trying to find the controller named B, but it's a directive: https://docs.angularjs.org/error/$compile/ctreq
Also, should I manipulate elements from within the link
function, or the directives controller? Even after reading, I'm still a bit fuzzy on link
vs controller
; the way I see it: link
is like a constructor of sorts, whereas the controller is where behavior goes.
So does that mean I always place DOM manipulation inside of the controller?
Is there any other way to approach this? I've read a bit about $scope
, but still not entirely clear on that either.