Search code examples
angularjsangularjs-directiveangularjs-ng-repeat

Bind event to child element of directive in link function


I need to bind an event to directive's children which is prepared with ng-repeat in templateUrl. I am trying to bind the event in link function but the children are not yet prepared.

Here is the plunker.

Here I want to bind click event on li tag which are prepared with ng-repeat.But by the time, the link is executed, the li elements are not yet prepared.

enter image description here


Solution

  • I've resolved the same problem with the angular $timeout

    link: function (scope, element) {
        $timeout(function () {
            element.on('click', '#Id', function () {
                console.log('inside event handler of the first child)
            })
        })
    }

    Try this by injecting $timeout in your directive