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

fix(compile): directive controller order fix #2738

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
39 changes: 29 additions & 10 deletions src/ng/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -985,13 +985,14 @@ function $CompileProvider($provide) {
}),
templateUrl = (isFunction(origAsyncDirective.templateUrl))
? origAsyncDirective.templateUrl($compileNode, tAttrs)
: origAsyncDirective.templateUrl;
: origAsyncDirective.templateUrl,
contentTemplateLinkFn;

$compileNode.html('');

$http.get(templateUrl, {cache: $templateCache}).
success(function(content) {
var compileNode, tempTemplateAttrs, $template;
var compileNode, tempTemplateAttrs, $template, contentDirectives;

content = denormalizeTemplate(content);

Expand All @@ -1005,7 +1006,7 @@ function $CompileProvider($provide) {

tempTemplateAttrs = {$attr: {}};
replaceWith($rootElement, $compileNode, compileNode);
collectDirectives(compileNode, directives, tempTemplateAttrs);
contentDirectives = collectDirectives(compileNode, [], tempTemplateAttrs);
mergeTemplateAttributes(tAttrs, tempTemplateAttrs);
} else {
compileNode = beforeTemplateCompileNode;
Expand All @@ -1014,9 +1015,11 @@ function $CompileProvider($provide) {

directives.unshift(derivedSyncDirective);
afterTemplateNodeLinkFn = applyDirectivesToNode(directives, compileNode, tAttrs, childTranscludeFn);
if (contentDirectives) {
contentTemplateLinkFn = applyDirectivesToNode(contentDirectives, compileNode, tAttrs, childTranscludeFn);
}
afterTemplateChildLinkFn = compileNodes($compileNode[0].childNodes, childTranscludeFn);


while(linkQueue.length) {
var scope = linkQueue.shift(),
beforeTemplateLinkNode = linkQueue.shift(),
Expand All @@ -1030,9 +1033,17 @@ function $CompileProvider($provide) {
replaceWith(linkRootElement, jqLite(beforeTemplateLinkNode), linkNode);
}

afterTemplateNodeLinkFn(function() {
beforeTemplateNodeLinkFn(afterTemplateChildLinkFn, scope, linkNode, $rootElement, controller);
}, scope, linkNode, $rootElement, controller);
if (contentTemplateLinkFn) {
afterTemplateNodeLinkFn(function() {
beforeTemplateNodeLinkFn(function () {
contentTemplateLinkFn(afterTemplateChildLinkFn, scope, linkNode, $rootElement, controller);
}, scope, linkNode, $rootElement, controller);
}, scope, linkNode, $rootElement, controller);
} else {
afterTemplateNodeLinkFn(function() {
beforeTemplateNodeLinkFn(afterTemplateChildLinkFn, scope, linkNode, $rootElement, controller);
}, scope, linkNode, $rootElement, controller);
}
}
linkQueue = null;
}).
Expand All @@ -1047,9 +1058,17 @@ function $CompileProvider($provide) {
linkQueue.push(rootElement);
linkQueue.push(controller);
} else {
afterTemplateNodeLinkFn(function() {
beforeTemplateNodeLinkFn(afterTemplateChildLinkFn, scope, node, rootElement, controller);
}, scope, node, rootElement, controller);
if (contentTemplateLinkFn) {
afterTemplateNodeLinkFn(function() {
beforeTemplateNodeLinkFn(function () {
contentTemplateLinkFn(afterTemplateChildLinkFn, scope, node, $rootElement, controller);
}, scope, node, $rootElement, controller);
}, scope, node, $rootElement, controller);
} else {
afterTemplateNodeLinkFn(function() {
beforeTemplateNodeLinkFn(afterTemplateChildLinkFn, scope, node, $rootElement, controller);
}, scope, node, $rootElement, controller);
}
}
};
}
Expand Down
44 changes: 39 additions & 5 deletions test/ng/compileSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1072,8 +1072,8 @@ describe('$compile', function() {
$httpBackend.flush();
$rootScope.$digest();
expect(log).toEqual(
'iFirst-C; FLUSH; iSecond-C; iThird-C; iLast-C; ' +
'iFirst-L; iSecond-L; iThird-L; iLast-L');
'iFirst-C; FLUSH; iSecond-C; iLast-C; iThird-C; ' +
'iThird-L; iFirst-L; iSecond-L; iLast-L');

var div = element.find('div');
expect(div.attr('i-first')).toEqual('');
Expand Down Expand Up @@ -1117,13 +1117,13 @@ describe('$compile', function() {
expect(log).toEqual('iFirst-C');
log('FLUSH');
$httpBackend.flush();
expect(log).toEqual('iFirst-C; FLUSH; iSecond-C; iThird-C; iLast-C');
expect(log).toEqual('iFirst-C; FLUSH; iSecond-C; iLast-C; iThird-C');

element = template($rootScope);
$rootScope.$digest();
expect(log).toEqual(
'iFirst-C; FLUSH; iSecond-C; iThird-C; iLast-C; ' +
'iFirst-L; iSecond-L; iThird-L; iLast-L');
'iFirst-C; FLUSH; iSecond-C; iLast-C; iThird-C; ' +
'iThird-L; iFirst-L; iSecond-L; iLast-L');

var div = element.find('div');
expect(div.attr('i-first')).toEqual('');
Expand Down Expand Up @@ -1981,6 +1981,31 @@ describe('$compile', function() {
scope: { attr: 'xxx' }
};
});
directive('parentDirective', function() {
return {
restrict: 'E',
transclude: true,
replace: true,
templateUrl: 'parentDirective.html',
scope: { param: '=' },
controller: function ($scope, $element, log) { log($scope.param); }
};
});
directive('childDirective', function() {
return {
restrict : 'E',
require: '^parentDirective',
scope: { otherParam: '@' },
link: function ($scope, $elem, $attrs, parentDirective) { },
replace : true,
templateUrl: 'childDirective.html',
controller : function($scope, $attrs, log) { log($scope.otherParam); }
};
});
}));
beforeEach(inject(function($templateCache) {
$templateCache.put('parentDirective.html', '<div ng-transclude></div>');
$templateCache.put('childDirective.html', '<span>{{otherParam}}</span>');
}));

describe('attribute', function() {
Expand Down Expand Up @@ -2008,6 +2033,15 @@ describe('$compile', function() {
expect(componentScope.attr).toEqual('hello igor');
expect(componentScope.attrAlias).toEqual('hello igor');
}));
it('should invoke the controller of the element directive before the controllers of the ' +
'attribute directives at the top level element of a templateUrl', inject(function($rootScope, $compile, log) {
$rootScope.name = 'lucas';
var element = $compile('<div><parent-directive param="name"><child-directive other-param=":-)"></child-directive></parentDirective></div>')($rootScope);
$rootScope.$apply();
expect(element.text()).toBe(":-)");
expect(log).toEqual('lucas; :-)');
dealoc(element);
}));
});


Expand Down