I'm currently working on a simple module to display notifications to the user in the form of toasts, or popups, that appear in the lower right-hand corner of the screen. The following directive works great except I would prefer to
a) use .bind
instead of ng-click
to handle manual dismissing of a notification;
b) use replace: true
help reduce some of the generated markup
toast.directive('toast', function() {
return {
link: function($scope, $element, $attrs, $location) {
$scope.delayDismiss = function(index) {
setTimeout(function() {
$scope.dismiss(index);
$scope.$apply();
}, 5000);
}
$scope.dismiss = function(index) {
$scope.messages[index].dismissed = true;
}
$element.bind('click', function() {
console.log('This never triggers');
});
},
replace: true,
restrict: 'E',
template: '<li ng-repeat="toast in messages" ng-click="dismiss($index)" ng-init="delayDismiss($index)" ng-if="!toast.dismissed">{{toast.sender}} says {{toast.message}}</li>'
}
});
The problem is that, now, $element
in my link()
function refers to the generated Angular comment instead of the <li>
resulting in the bound click event never triggering. Which doesn't make any sense to me.
Am I misunderstanding this whole directive thing?