1

i've created a directive and i'm not quite sure about the binding like element.bind("click", function(){}). The link function of each directive gets called multiple times and each call will produce a duplicate binding. What is the most Angular way to accomplish this (even the click binding is available as attribute) ?

var globalCounter = 0;
app.directive("myDirective", function()
{
    return {
       link: function(scope, element) {
          globalCounter++;
          $(element).bind("click", function () {});
       }
    }
});

The globalCounter variable (and the click bindings ?) will increase every time i change the ng-view to a different template.

Maybe the element gets destroyed and the binding with it, i'm not sure, maybe this is my answer.

1
  • 1
    pls post fiddle or plunker of demo you have tried Commented Sep 24, 2013 at 10:49

4 Answers 4

1

Your assumption are not completely correct. The directive link function gets invoked once per directive declaration. So the element on which click is getting attached is different. Therefore each function is acting on a different control.

You can still do css selector based click action binding, but then the directive should be defined on the parent.

Also when the view changes the DOM is destroyed and hence you old binding will get removed.

Sign up to request clarification or add additional context in comments.

Comments

1

I think you can set a restrict config option

First, we’re setting the restrict config option. The restrict option is used to specify how a directive can be invoked on the page.

As we saw before, there are four different ways to invoke a directive, so there are four valid options for restrict:

'A' - <span ng-sparkline></span>
'E' - <ng-sparkline></ng-sparkline>
'C' - <span class="ng-sparkline"></span>
'M' - <!-- directive: ng-sparkline -->

http://www.ng-newsletter.com/posts/directives.html

1 Comment

Just left this to make this example more simple.
1

You did not put the counter in the click event. Also, you do not need jQuery selector for the element that has the directive.

app.directive("myDirective", function()
{
    return {
       link: function(scope, element, attrs) {
          element.bind("click", function () {
             // Click event here
          });
       }
    }
});

2 Comments

its still the same problem. The link function gets called multible time. Each time a new bind is done to the element...
How are you binding it to the element? Can you show the HTML snippet containing the directive?
0

i wrote little example of this derective https://github.com/JTOne123/angularJs.elementReady/blob/master/src/onElementReady.js and fix here some problems with scope

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.