Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 9c51d50

Browse files
jankucaIgorMinar
authored andcommitted
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
1 parent 1c3a46a commit 9c51d50

File tree

2 files changed

+72
-8
lines changed

2 files changed

+72
-8
lines changed

src/ng/compile.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -621,13 +621,6 @@ function $CompileProvider($provide) {
621621

622622
directiveName = directive.name;
623623

624-
if (directiveValue = directive.controller) {
625-
controllerDirectives = controllerDirectives || {};
626-
assertNoDuplicate("'" + directiveName + "' controller",
627-
controllerDirectives[directiveName], directive, $compileNode);
628-
controllerDirectives[directiveName] = directive;
629-
}
630-
631624
if (directiveValue = directive.transclude) {
632625
assertNoDuplicate('transclusion', transcludeDirective, directive, $compileNode);
633626
transcludeDirective = directive;
@@ -705,6 +698,13 @@ function $CompileProvider($provide) {
705698
}
706699
}
707700

701+
if (!directive.templateUrl && directive.controller) {
702+
controllerDirectives = controllerDirectives || {};
703+
assertNoDuplicate("'" + directiveName + "' controller",
704+
controllerDirectives[directiveName], directive, $compileNode);
705+
controllerDirectives[directiveName] = directive;
706+
}
707+
708708
if (directive.terminal) {
709709
nodeLinkFn.terminal = true;
710710
terminalPriority = Math.max(terminalPriority, directive.priority);
@@ -962,7 +962,7 @@ function $CompileProvider($provide) {
962962
origAsyncDirective = directives.shift(),
963963
// The fact that we have to copy and patch the directive seems wrong!
964964
derivedSyncDirective = extend({}, origAsyncDirective, {
965-
controller: null, templateUrl: null, transclude: null, scope: null
965+
templateUrl: null, transclude: null, scope: null
966966
});
967967

968968
$compileNode.html('');

test/ng/compileSpec.js

+64
Original file line numberDiff line numberDiff line change
@@ -2305,6 +2305,70 @@ describe('$compile', function() {
23052305
expect(element.text()).toBe('parentTemplateText;childTemplateText;childContentText;babyTemplateText;')
23062306
});
23072307
});
2308+
2309+
2310+
it('should allow controller usage in pre-link directive functions with templateUrl', function () {
2311+
module(function () {
2312+
var Ctrl = function (log) {
2313+
log('instance');
2314+
};
2315+
2316+
directive('myDirective', function () {
2317+
return {
2318+
scope: true,
2319+
templateUrl: 'hello.html',
2320+
controller: Ctrl,
2321+
compile: function () {
2322+
return {
2323+
pre: function (scope, template, attr, ctrl) {},
2324+
post: function () {}
2325+
};
2326+
}
2327+
};
2328+
});
2329+
});
2330+
2331+
inject(function ($templateCache, $compile, $rootScope, log) {
2332+
$templateCache.put('hello.html', '<p>Hello</p>');
2333+
2334+
element = $compile('<div my-directive></div>')($rootScope);
2335+
$rootScope.$apply();
2336+
2337+
expect(log).toEqual('instance');
2338+
expect(element.text()).toBe('Hello');
2339+
});
2340+
});
2341+
2342+
2343+
it('should allow controller usage in pre-link directive functions with a template', function () {
2344+
module(function () {
2345+
var Ctrl = function (log) {
2346+
log('instance');
2347+
};
2348+
2349+
directive('myDirective', function () {
2350+
return {
2351+
scope: true,
2352+
template: '<p>Hello</p>',
2353+
controller: Ctrl,
2354+
compile: function () {
2355+
return {
2356+
pre: function (scope, template, attr, ctrl) {},
2357+
post: function () {}
2358+
};
2359+
}
2360+
};
2361+
});
2362+
});
2363+
2364+
inject(function ($templateCache, $compile, $rootScope, log) {
2365+
element = $compile('<div my-directive></div>')($rootScope);
2366+
$rootScope.$apply();
2367+
2368+
expect(log).toEqual('instance');
2369+
expect(element.text()).toBe('Hello');
2370+
});
2371+
});
23082372
});
23092373

23102374

0 commit comments

Comments
 (0)