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

fix(compile): fix directives with controller, templateUrl and compile properties #3514

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/ng/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -782,13 +782,6 @@ function $CompileProvider($provide) {

directiveName = directive.name;

if (directiveValue = directive.controller) {
controllerDirectives = controllerDirectives || {};
assertNoDuplicate("'" + directiveName + "' controller",
controllerDirectives[directiveName], directive, $compileNode);
controllerDirectives[directiveName] = directive;
}

if (directiveValue = directive.transclude) {
assertNoDuplicate('transclusion', transcludeDirective, directive, $compileNode);
transcludeDirective = directive;
Expand Down Expand Up @@ -877,6 +870,13 @@ function $CompileProvider($provide) {
}
}

if (!directive.templateUrl && directive.controller) {
controllerDirectives = controllerDirectives || {};
assertNoDuplicate("'" + directiveName + "' controller",
controllerDirectives[directiveName], directive, $compileNode);
controllerDirectives[directiveName] = directive;
}

if (directive.terminal) {
nodeLinkFn.terminal = true;
terminalPriority = Math.max(terminalPriority, directive.priority);
Expand Down Expand Up @@ -1157,7 +1157,7 @@ function $CompileProvider($provide) {
origAsyncDirective = directives.shift(),
// The fact that we have to copy and patch the directive seems wrong!
derivedSyncDirective = extend({}, origAsyncDirective, {
controller: null, templateUrl: null, transclude: null, scope: null, replace: null
templateUrl: null, transclude: null, scope: null, replace: null
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this doesn't look right because it will cause the controller to be instantiated only after the template arrives.

the original code instantiated controller even before the template defined via templateUrl arrived. We should preserve the timing.

}),
templateUrl = (isFunction(origAsyncDirective.templateUrl))
? origAsyncDirective.templateUrl($compileNode, tAttrs)
Expand Down
62 changes: 62 additions & 0 deletions test/ng/compileSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2502,6 +2502,68 @@ describe('$compile', function() {
expect(element.text()).toBe('parentTemplateText;childTemplateText;childContentText;babyTemplateText;')
});
});

it('should allow controller usage in directives 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', '<p>Hello</p>');

element = $compile('<div my-directive></div>')($rootScope);
$rootScope.$apply();

expect(log).toEqual('instance');
expect(element.text()).toBe('Hello');
});
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you write an additional test that will be the same except for using template instead of templateUrl?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added


it('should allow controller usage in directives with a template', function () {
module(function () {
var Ctrl = function (log) {
log('instance');
};

directive('myDirective', function () {
return {
scope: true,
template: '<p>Hello</p>',
controller: Ctrl,
compile: function () {
return {
pre: function (scope, template, attr, ctrl) {},
post: function () {}
};
}
};
});
});

inject(function ($templateCache, $compile, $rootScope, log) {
element = $compile('<div my-directive></div>')($rootScope);
$rootScope.$apply();

expect(log).toEqual('instance');
expect(element.text()).toBe('Hello');
});
});
});


Expand Down