Frage

i have a strange behavior in my code with custom directive. The link function is called multiple times but as example if want to initialize a third-part library (example maps) this behavior breaks the lib.

link: function(scope) {
    alert("invoked");
}

http://jsfiddle.net/dYs8L/1/

jsfiddle updated with the correct code

http://jsfiddle.net/dYs8L/2/

War es hilfreich?

Lösung

It's working as designed...ng-repeat essentially creates a new copy the markup with an isolated scope for every item in the collection. If you need to initialize something once, you'll need to do it elsewhere. You can consider putting this resource into a service; that will allow all the elements output by ng-repeat to use the same instance.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top