Issue
I have a directive that expands and closes the height of a div. Its used multiple times, so when the user opens one box I want to close other boxes that are open. I figured it would be best to emit an event, but the problem is that the directive then closes itself as well, so no div can be open.
Can I ignore a message that comes from the same directive?
angular.module('cinemaApp')
.directive('expand', function ($rootScope) {
return {
restrict: 'A',
link: function postLink(scope, element, attrs) {
$rootScope.$on('contract', function(payload) {
element.css('height', attrs.fixedheight);
});
scope.$watch('open', function(value) {
element.css('height', (value ? 'auto' : attrs.fixedheight));
$rootScope.$emit('contract');
});
element.css('height', attrs.fixedheight);
}
};
});
Solution
When you emit the event, you can pass additional arguments that are given to the listeners. So you can emit like
$rootScope.$emit('contract', element);
And listen like
$rootScope.$on('contract', function(event, target) {
if (target != element)
element.css('height', attrs.fixedheight);
});
Answered By - Chris Bouchard
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.