From 9c51d503188efae14b81bd4d6dd7d5a3363f050f Mon Sep 17 00:00:00 2001 From: jankuca Date: Thu, 8 Aug 2013 16:04:11 -0700 Subject: fix($compile): always instantiate controllers before pre-link fns run Controllers should be always instantiated after compile fn runs, but before pre-link fn runs. This way, controllers are available to pre-link fns that request them. Previously this was broken for async directives (directives with templateUrl). Closes #3493 Closes #3482 Closes #3514 --- test/ng/compileSpec.js | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) (limited to 'test/ng/compileSpec.js') diff --git a/test/ng/compileSpec.js b/test/ng/compileSpec.js index 12b51fb4..93fe6ced 100755 --- a/test/ng/compileSpec.js +++ b/test/ng/compileSpec.js @@ -2305,6 +2305,70 @@ describe('$compile', function() { expect(element.text()).toBe('parentTemplateText;childTemplateText;childContentText;babyTemplateText;') }); }); + + + it('should allow controller usage in pre-link directive functions with templateUrl', function () { + module(function () { + var Ctrl = function (log) { + log('instance'); + }; + + directive('myDirective', function () { + return { + scope: true, + templateUrl: 'hello.html', + controller: Ctrl, + compile: function () { + return { + pre: function (scope, template, attr, ctrl) {}, + post: function () {} + }; + } + }; + }); + }); + + inject(function ($templateCache, $compile, $rootScope, log) { + $templateCache.put('hello.html', '
Hello
'); + + element = $compile('')($rootScope); + $rootScope.$apply(); + + expect(log).toEqual('instance'); + expect(element.text()).toBe('Hello'); + }); + }); + + + it('should allow controller usage in pre-link directive functions with a template', function () { + module(function () { + var Ctrl = function (log) { + log('instance'); + }; + + directive('myDirective', function () { + return { + scope: true, + template: 'Hello
', + controller: Ctrl, + compile: function () { + return { + pre: function (scope, template, attr, ctrl) {}, + post: function () {} + }; + } + }; + }); + }); + + inject(function ($templateCache, $compile, $rootScope, log) { + element = $compile('')($rootScope); + $rootScope.$apply(); + + expect(log).toEqual('instance'); + expect(element.text()).toBe('Hello'); + }); + }); }); -- cgit v1.2.3