From 843f762c573e38a044f920c5575c6feb46bc7226 Mon Sep 17 00:00:00 2001 From: Igor Minar Date: Wed, 2 May 2012 16:04:11 -0700 Subject: fix($compile): prevent duplicate directive controller instantiation Closes #876 --- test/ng/compileSpec.js | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'test') diff --git a/test/ng/compileSpec.js b/test/ng/compileSpec.js index 1aef24fe..54837bf7 100644 --- a/test/ng/compileSpec.js +++ b/test/ng/compileSpec.js @@ -1692,7 +1692,45 @@ describe('$compile', function() { element = $compile('
')($rootScope); expect(log).toEqual('dep:c1-c2'); }); + }); + + it('should instantiate the controller just once when template/templateUrl', function() { + var syncCtrlSpy = jasmine.createSpy('sync controller'), + asyncCtrlSpy = jasmine.createSpy('async controller'); + + module(function($compileProvider) { + $compileProvider.directive('myDirectiveSync', valueFn({ + template: '
Hello!
', + controller: syncCtrlSpy + })); + $compileProvider.directive('myDirectiveAsync', valueFn({ + templateUrl: 'myDirectiveAsync.html', + controller: asyncCtrlSpy, + compile: function() { + return function() { + } + } + })); + }); + + inject(function($templateCache, $compile, $rootScope) { + expect(syncCtrlSpy).not.toHaveBeenCalled(); + expect(asyncCtrlSpy).not.toHaveBeenCalled(); + + $templateCache.put('myDirectiveAsync.html', '
Hello!
'); + element = $compile('
'+ + '' + + '' + + '
')($rootScope); + expect(syncCtrlSpy).not.toHaveBeenCalled(); + expect(asyncCtrlSpy).not.toHaveBeenCalled(); + + $rootScope.$apply(); + + //expect(syncCtrlSpy).toHaveBeenCalledOnce(); + expect(asyncCtrlSpy).toHaveBeenCalledOnce(); + }); }); }); -- cgit v1.2.3