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/

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top