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

Commit 5c56011

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 4175377 commit 5c56011

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
@@ -782,13 +782,6 @@ function $CompileProvider($provide) {
782782

783783
directiveName = directive.name;
784784

785-
if (directiveValue = directive.controller) {
786-
controllerDirectives = controllerDirectives || {};
787-
assertNoDuplicate("'" + directiveName + "' controller",
788-
controllerDirectives[directiveName], directive, $compileNode);
789-
controllerDirectives[directiveName] = directive;
790-
}
791-
792785
if (directiveValue = directive.transclude) {
793786
assertNoDuplicate('transclusion', transcludeDirective, directive, $compileNode);
794787
transcludeDirective = directive;
@@ -877,6 +870,13 @@ function $CompileProvider($provide) {
877870
}
878871
}
879872

873+
if (!directive.templateUrl && directive.controller) {
874+
controllerDirectives = controllerDirectives || {};
875+
assertNoDuplicate("'" + directiveName + "' controller",
876+
controllerDirectives[directiveName], directive, $compileNode);
877+
controllerDirectives[directiveName] = directive;
878+
}
879+
880880
if (directive.terminal) {
881881
nodeLinkFn.terminal = true;
882882
terminalPriority = Math.max(terminalPriority, directive.priority);
@@ -1157,7 +1157,7 @@ function $CompileProvider($provide) {
11571157
origAsyncDirective = directives.shift(),
11581158
// The fact that we have to copy and patch the directive seems wrong!
11591159
derivedSyncDirective = extend({}, origAsyncDirective, {
1160-
controller: null, templateUrl: null, transclude: null, scope: null, replace: null
1160+
templateUrl: null, transclude: null, scope: null, replace: null
11611161
}),
11621162
templateUrl = (isFunction(origAsyncDirective.templateUrl))
11631163
? origAsyncDirective.templateUrl($compileNode, tAttrs)

test/ng/compileSpec.js

+64
Original file line numberDiff line numberDiff line change
@@ -2502,6 +2502,70 @@ describe('$compile', function() {
25022502
expect(element.text()).toBe('parentTemplateText;childTemplateText;childContentText;babyTemplateText;')
25032503
});
25042504
});
2505+
2506+
2507+
it('should allow controller usage in pre-link directive functions with templateUrl', function () {
2508+
module(function () {
2509+
var Ctrl = function (log) {
2510+
log('instance');
2511+
};
2512+
2513+
directive('myDirective', function () {
2514+
return {
2515+
scope: true,
2516+
templateUrl: 'hello.html',
2517+
controller: Ctrl,
2518+
compile: function () {
2519+
return {
2520+
pre: function (scope, template, attr, ctrl) {},
2521+
post: function () {}
2522+
};
2523+
}
2524+
};
2525+
});
2526+
});
2527+
2528+
inject(function ($templateCache, $compile, $rootScope, log) {
2529+
$templateCache.put('hello.html', '<p>Hello</p>');
2530+
2531+
element = $compile('<div my-directive></div>')($rootScope);
2532+
$rootScope.$apply();
2533+
2534+
expect(log).toEqual('instance');
2535+
expect(element.text()).toBe('Hello');
2536+
});
2537+
});
2538+
2539+
2540+
it('should allow controller usage in pre-link directive functions with a template', function () {
2541+
module(function () {
2542+
var Ctrl = function (log) {
2543+
log('instance');
2544+
};
2545+
2546+
directive('myDirective', function () {
2547+
return {
2548+
scope: true,
2549+
template: '<p>Hello</p>',
2550+
controller: Ctrl,
2551+
compile: function () {
2552+
return {
2553+
pre: function (scope, template, attr, ctrl) {},
2554+
post: function () {}
2555+
};
2556+
}
2557+
};
2558+
});
2559+
});
2560+
2561+
inject(function ($templateCache, $compile, $rootScope, log) {
2562+
element = $compile('<div my-directive></div>')($rootScope);
2563+
$rootScope.$apply();
2564+
2565+
expect(log).toEqual('instance');
2566+
expect(element.text()).toBe('Hello');
2567+
});
2568+
});
25052569
});
25062570

25072571

0 commit comments

Comments
 (0)