The factory directive itself is single. Everything that you declare outside the definition object will be global for all instances. Because each instance has its own scope, instance data must fall within the scope. So something like this:
angular.module("myApp", []) .directive("myDir", function() { var myGlobal = 0; return { template: '<div>Global: {{getGlobal()}}, Local: {{local}} -- <a href="" ng-click="increment()">Increment</a></div>', scope: {}, link: function(scope, element, attrs) { scope.local = 0; scope.increment = function() { scope.local++; myGlobal++; } scope.getGlobal = function() { return myGlobal; } } } });
http://jsfiddle.net/7YwDS/
dnc253
source share