Skip to content

Commit 57a972d

Browse files
ksvitkovskyNarretz
ksvitkovsky
authored andcommitted
feat($compile): overload .component() to accept object map of components
Register multiple components with single call as it is possible with other module units. Closes angular#14579 Closes angular#16062
1 parent cdaa6a9 commit 57a972d

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

src/ng/compile.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -1169,7 +1169,8 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
11691169
* @ngdoc method
11701170
* @name $compileProvider#component
11711171
* @module ng
1172-
* @param {string} name Name of the component in camelCase (i.e. `myComp` which will match `<my-comp>`)
1172+
* @param {string|Object} name Name of the component in camelCase (i.e. `myComp` which will match `<my-comp>`),
1173+
* or an object map of components where the keys are the names and the values are the component definition objects.
11731174
* @param {Object} options Component definition object (a simplified
11741175
* {@link ng.$compile#directive-definition-object directive definition object}),
11751176
* with the following properties (all optional):
@@ -1252,6 +1253,11 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
12521253
* See also {@link ng.$compileProvider#directive $compileProvider.directive()}.
12531254
*/
12541255
this.component = function registerComponent(name, options) {
1256+
if (!isString(name)) {
1257+
forEach(name, reverseParams(bind(this, registerComponent)));
1258+
return this;
1259+
}
1260+
12551261
var controller = options.controller || function() {};
12561262

12571263
function factory($injector) {

test/ng/compileSpec.js

+29
Original file line numberDiff line numberDiff line change
@@ -12089,6 +12089,7 @@ describe('$compile', function() {
1208912089
it('should return the module', function() {
1209012090
var myModule = angular.module('my', []);
1209112091
expect(myModule.component('myComponent', {})).toBe(myModule);
12092+
expect(myModule.component({})).toBe(myModule);
1209212093
});
1209312094

1209412095
it('should register a directive', function() {
@@ -12107,6 +12108,34 @@ describe('$compile', function() {
1210712108
});
1210812109
});
1210912110

12111+
it('should register multiple directives when object passed as first parameter', function() {
12112+
var log = '';
12113+
angular.module('my', []).component({
12114+
fooComponent: {
12115+
template: '<div>FOO SUCCESS</div>',
12116+
controller: function() {
12117+
log += 'FOO:OK';
12118+
}
12119+
},
12120+
barComponent: {
12121+
template: '<div>BAR SUCCESS</div>',
12122+
controller: function() {
12123+
log += 'BAR:OK';
12124+
}
12125+
}
12126+
});
12127+
module('my');
12128+
12129+
inject(function($compile, $rootScope) {
12130+
var fooElement = $compile('<foo-component></foo-component>')($rootScope);
12131+
var barElement = $compile('<bar-component></bar-component>')($rootScope);
12132+
12133+
expect(fooElement.find('div').text()).toEqual('FOO SUCCESS');
12134+
expect(barElement.find('div').text()).toEqual('BAR SUCCESS');
12135+
expect(log).toEqual('FOO:OKBAR:OK');
12136+
});
12137+
});
12138+
1211012139
it('should register a directive via $compileProvider.component()', function() {
1211112140
module(function($compileProvider) {
1211212141
$compileProvider.component('myComponent', {

0 commit comments

Comments
 (0)