I am using carousel (https://market.ionic.io/plugins/morph-carousel) in an ionic project. This is a custom angularjs directive. I am using 2 carousels in one screen. Now, what I want is that on change of 1st carousel the second carousel should also get updated with the new value. And to do this the 2nd carousel custom directive has to be reloaded/refreshed. Is there a way to refresh/reload custom directives in angularjs?
1 Answer
Add a watcher in first directive in link function.
scope.$watch('thingToWatchForRefershing', function (newValue, oldValue) {
if (!newValue || angular.equals(newValue, oldValue)) {
return;
}
scope.$emit('somethingChangedInFirstDirective', dataToBePassed);
});
Now, in your second directive, have a render function which when called will refresh the models and views and also add a listener.
link: function () {
scope.render = function () {
// all logic goes here
};
scope.$on('somethingChangedInFirstDirective', function (ev, data) {
// render everything again i.e. reload the directive
scope.render();
});
// first time rendering
scope.render();
}
Hope it helps :)
Update:
- Pass an attribute to the first directive only. eg:
<custom-carousel data-is-to-be-watched="isToBeWatched" />and in your controller set:$scope.isToBeWatched = true. Also, for the second directive use:<custom-carousel data-is-tobe-reloaded="isToBeReloaded" />and in the same controller initialize:$scope.isToBeReloaded = false; - If its an isolated directive, having its own scope, then:
have this
scope:{isToBeWatched: '=?', isToBeReloaded: '=?'}, link: function (...) {...} - Once the directive is loaded, check
if (scope.isToBeWatched), is true, it means first directive is loaded/changed since we passed the attr only in first directive. Now, emit an event, which will be listened in the controller. In the listener do:$scope.isToBeReloaded = true;which in turn will set the variableisToBeReloadedin the second directive's scope, since we passed in 2nd directive. - Have a watcher for
isToBeReloadedand reload the render fn as I mentioned earlier.
Rough code:
Controller:
$scope.isToBeWatched = true;
$scope.isToBeReloaded = false;
$scope.$on('somethingChangedInFirstDirective', function () {
$scope.isToBeReloaded = true;
});
Directive:
scope: {
isToBeWatched: '=?',
isToBeReloaded: '=?'
},
link: function (scope) {
scope.render = function () {
if (scope.isToBeWatched) {
scope.$emit('somethingChangedInFirstDirective');
}
};
scope.render();
scope.$watch('isToBeReloaded', function (newValue, oldValue) {
if (!newValue || angular.equals(newValue, oldValue))
return;
scope.render();
})
}
4 Comments
anuj trehan
But its the same directive which is being used twice. How can we have separate link function for them?
anuj trehan
I was able to call the link function again, but I also need to reload the template. How can this be done?
softvar
Using double binding in your template, so that whenever your controller/directive updates, view changes automatically using ng-bind/ {{...}}
softvar
Glad to hear that :)
linkfunction of the second directive you can use$scope.watchto track the changes of the first directive model variables?