mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-03-16 23:30:23 +00:00
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
This commit is contained in:
parent
4175377aaf
commit
5c56011742
2 changed files with 72 additions and 8 deletions
|
|
@ -782,13 +782,6 @@ function $CompileProvider($provide) {
|
||||||
|
|
||||||
directiveName = directive.name;
|
directiveName = directive.name;
|
||||||
|
|
||||||
if (directiveValue = directive.controller) {
|
|
||||||
controllerDirectives = controllerDirectives || {};
|
|
||||||
assertNoDuplicate("'" + directiveName + "' controller",
|
|
||||||
controllerDirectives[directiveName], directive, $compileNode);
|
|
||||||
controllerDirectives[directiveName] = directive;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (directiveValue = directive.transclude) {
|
if (directiveValue = directive.transclude) {
|
||||||
assertNoDuplicate('transclusion', transcludeDirective, directive, $compileNode);
|
assertNoDuplicate('transclusion', transcludeDirective, directive, $compileNode);
|
||||||
transcludeDirective = directive;
|
transcludeDirective = directive;
|
||||||
|
|
@ -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) {
|
if (directive.terminal) {
|
||||||
nodeLinkFn.terminal = true;
|
nodeLinkFn.terminal = true;
|
||||||
terminalPriority = Math.max(terminalPriority, directive.priority);
|
terminalPriority = Math.max(terminalPriority, directive.priority);
|
||||||
|
|
@ -1157,7 +1157,7 @@ function $CompileProvider($provide) {
|
||||||
origAsyncDirective = directives.shift(),
|
origAsyncDirective = directives.shift(),
|
||||||
// The fact that we have to copy and patch the directive seems wrong!
|
// The fact that we have to copy and patch the directive seems wrong!
|
||||||
derivedSyncDirective = extend({}, origAsyncDirective, {
|
derivedSyncDirective = extend({}, origAsyncDirective, {
|
||||||
controller: null, templateUrl: null, transclude: null, scope: null, replace: null
|
templateUrl: null, transclude: null, scope: null, replace: null
|
||||||
}),
|
}),
|
||||||
templateUrl = (isFunction(origAsyncDirective.templateUrl))
|
templateUrl = (isFunction(origAsyncDirective.templateUrl))
|
||||||
? origAsyncDirective.templateUrl($compileNode, tAttrs)
|
? origAsyncDirective.templateUrl($compileNode, tAttrs)
|
||||||
|
|
|
||||||
|
|
@ -2502,6 +2502,70 @@ describe('$compile', function() {
|
||||||
expect(element.text()).toBe('parentTemplateText;childTemplateText;childContentText;babyTemplateText;')
|
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', '<p>Hello</p>');
|
||||||
|
|
||||||
|
element = $compile('<div my-directive></div>')($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: '<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');
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue