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

Commit

Permalink
fix($compile): always instantiate controllers in parent->child order
Browse files Browse the repository at this point in the history
Previously it was possible to get into a situation where child controller
was being instantiated before parent which resulted in an error.

Closes #2738
  • Loading branch information
IgorMinar committed Jul 22, 2013
1 parent 3967f5f commit 45f9f62
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/ng/directive/ngTransclude.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,14 @@
*
*/
var ngTranscludeDirective = ngDirective({
controller: ['$transclude', '$element', function($transclude, $element) {
$transclude(function(clone) {
$element.append(clone);
controller: ['$transclude', '$element', '$scope', function($transclude, $element, $scope) {
// use evalAsync so that we don't process transclusion before directives on the parent element even when the
// transclusion replaces the current element. (we can't use priority here because that applies only to compile fns
// and not controllers
$scope.$evalAsync(function() {
$transclude(function(clone) {
$element.append(clone);
});
});
}]
});
89 changes: 89 additions & 0 deletions test/ng/compileSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2331,6 +2331,95 @@ describe('$compile', function() {
expect(asyncCtrlSpy).toHaveBeenCalledOnce();
});
});



it('should instantiate controllers in the parent->child order when transluction, templateUrl and replacement ' +
'are in the mix', function() {
// When a child controller is in the transclusion that replaces the parent element that has a directive with
// a controller, we should ensure that we first instantiate the parent and only then stuff that comes from the
// transclusion.
//
// The transclusion moves the child controller onto the same element as parent controller so both controllers are
// on the same level.

module(function() {
directive('parentDirective', function() {
return {
transclude: true,
replace: true,
templateUrl: 'parentDirective.html',
controller: function (log) { log('parentController'); }
};
});
directive('childDirective', function() {
return {
require: '^parentDirective',
templateUrl: 'childDirective.html',
controller : function(log) { log('childController'); }
};
});
});

inject(function($templateCache, log, $compile, $rootScope) {
$templateCache.put('parentDirective.html', '<div ng-transclude>parentTemplateText;</div>');
$templateCache.put('childDirective.html', '<span>childTemplateText;</span>');

element = $compile('<div parent-directive><div child-directive></div>childContentText;</div>')($rootScope);
$rootScope.$apply();
expect(log).toEqual('parentController; childController');
expect(element.text()).toBe('parentTemplateText;childTemplateText;childContentText;')
});
});


it('should instantiate controllers in the parent->child->baby order when nested transluction, templateUrl and ' +
'replacement are in the mix', function() {
// similar to the test above, except that we have one more layer of nesting and nested transclusion

module(function() {
directive('parentDirective', function() {
return {
transclude: true,
replace: true,
templateUrl: 'parentDirective.html',
controller: function (log) { log('parentController'); }
};
});
directive('childDirective', function() {
return {
require: '^parentDirective',
transclude: true,
replace: true,
templateUrl: 'childDirective.html',
controller : function(log) { log('childController'); }
};
});
directive('babyDirective', function() {
return {
require: '^childDirective',
templateUrl: 'babyDirective.html',
controller : function(log) { log('babyController'); }
};
});
});

inject(function($templateCache, log, $compile, $rootScope) {
$templateCache.put('parentDirective.html', '<div ng-transclude>parentTemplateText;</div>');
$templateCache.put('childDirective.html', '<span ng-transclude>childTemplateText;</span>');
$templateCache.put('babyDirective.html', '<span>babyTemplateText;</span>');

element = $compile('<div parent-directive>' +
'<div child-directive>' +
'childContentText;' +
'<div baby-directive>babyContent;</div>' +
'</div>' +
'</div>')($rootScope);
$rootScope.$apply();
expect(log).toEqual('parentController; childController; babyController');
expect(element.text()).toBe('parentTemplateText;childTemplateText;childContentText;babyTemplateText;')
});
});
});


Expand Down

1 comment on commit 45f9f62

@cristatus
Copy link

Choose a reason for hiding this comment

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

Fixes #1809

Please sign in to comment.