0

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?

Chris Wright
  • 754
  • 1
  • 8
  • 19
  • "The problem is that, now, `$element` in my `link()` function refers to the generated Angular comment" is wrong. As expected, `$element` refers to the `
  • `. Can you provide a fiddle reproducing the problem? Notice that `replace: true` is a deprecated functionality since 1.3.0-beta.10.
  • – Blackhole May 31 '14 at 18:19