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

feat($compile): add controllersReady option to component helper #13760

Closed
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
6 changes: 5 additions & 1 deletion src/ng/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -1072,7 +1072,11 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
transclude: options.transclude,
scope: {},
bindToController: options.bindings || {},
restrict: 'E'
restrict: 'E',
require: options.require,
link: options.controllersReady && function(scope, element, attrs, controllers) {
options.controllersReady.apply(options, isArray(controllers) ? controllers : [controllers]);
}
};
}

Expand Down
32 changes: 32 additions & 0 deletions test/ng/compileSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9435,5 +9435,37 @@ describe('$compile', function() {
}));
});
});

it('should call `controllersReady` with required controllers, if provided', function() {
var controllersReadySpy = jasmine.createSpy('controllersReady');
function MeController() { this.name = 'Me'; }
function ParentController() { this.name = 'Parent'; }
function SiblingController() { this.name = 'Sibling'; }

angular.module('my', [])
.component('me', {
require: ['me', '^parent', 'sibling'],
controllersReady: controllersReadySpy,
controller: MeController
})
.component('parent', {
controller: ParentController
})
.directive('sibling', function() {
return {
controller: SiblingController
};
});

module('my');
inject(function($compile, $rootScope, meDirective) {
element = $compile('<parent><me sibling></me></parent>')($rootScope);
expect(controllersReadySpy).toHaveBeenCalledWith(
jasmine.any(MeController),
jasmine.any(ParentController),
jasmine.any(SiblingController)
);
});
});
});
});