M
M
muzamy2015-02-17 17:40:20
Angular
muzamy, 2015-02-17 17:40:20

Directive and DOM event handling in AngularJS?

There is a directive that has a mousedown event

function evClick() {
    return {
        restrict: "AE",
        link: function (scope, element) {
            element.bind('mousedown', function (event) {
                console.log("mousedown");
            });
        }
    };
}

Let's add a part of the html code that contains our directive
angular.element(document.getElementById('foo')).append("<h2 ev-click>add element</h2>");

The directive does not work in the added code
How can I make the directive work in the added element?
Example
plnkr.co/edit/nUasLau3vv4WvMINMNT9?p=preview

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Bykov, 2015-02-17
@jusio

Angular does not know how to parse HTML on every change by the user. If you need to dynamically generate templates, then you should use the $compile service:
Example on plnkr

L
lega, 2015-02-22
@lega

If Angular.js isn't critical, here's an Angular Light example.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question