From 9e9cb5a3a7c2302b20b63bde5998411b7be9094e Mon Sep 17 00:00:00 2001 From: ksvitkovsky Date: Mon, 19 Jun 2017 17:50:28 +0400 Subject: [PATCH 1/3] test($compile): add test cases for "component" method overload Ensure that all components are being registered and the method returns a module instance when object is being used as first parameter Required for feature #14579 --- test/ng/compileSpec.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/test/ng/compileSpec.js b/test/ng/compileSpec.js index 62bd5841f3e8..4fa14d2daff0 100644 --- a/test/ng/compileSpec.js +++ b/test/ng/compileSpec.js @@ -12089,6 +12089,7 @@ describe('$compile', function() { it('should return the module', function() { var myModule = angular.module('my', []); expect(myModule.component('myComponent', {})).toBe(myModule); + expect(myModule.component({})).toBe(myModule); }); it('should register a directive', function() { @@ -12107,6 +12108,34 @@ describe('$compile', function() { }); }); + it('should register multiple directives when object passed as first parameter', function() { + var log = ''; + angular.module('my', []).component({ + fooComponent: { + template: '
FOO SUCCESS
', + controller: function() { + log += 'FOO:OK'; + } + }, + barComponent: { + template: '
BAR SUCCESS
', + controller: function() { + log += 'BAR:OK'; + } + } + }); + module('my'); + + inject(function($compile, $rootScope) { + var fooElement = $compile('')($rootScope); + var barElement = $compile('')($rootScope); + + expect(fooElement.find('div').text()).toEqual('FOO SUCCESS'); + expect(barElement.find('div').text()).toEqual('BAR SUCCESS'); + expect(log).toEqual('FOO:OKBAR:OK'); + }); + }); + it('should register a directive via $compileProvider.component()', function() { module(function($compileProvider) { $compileProvider.component('myComponent', { From 900840a4bf2b15e9a48bbc9d7a22816d5ba8d409 Mon Sep 17 00:00:00 2001 From: ksvitkovsky Date: Mon, 19 Jun 2017 18:10:24 +0400 Subject: [PATCH 2/3] feat($compile): overload "component" method to accept object map of components Register multiple components with single call as it is possible with other module units. As requested in #14579 --- src/ng/compile.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/ng/compile.js b/src/ng/compile.js index ef2e65ad379d..da1e65c56647 100644 --- a/src/ng/compile.js +++ b/src/ng/compile.js @@ -1169,7 +1169,8 @@ function $CompileProvider($provide, $$sanitizeUriProvider) { * @ngdoc method * @name $compileProvider#component * @module ng - * @param {string} name Name of the component in camelCase (i.e. `myComp` which will match ``) + * @param {string} name Name of the component in camelCase (i.e. `myComp` which will match ``), + * or an object map of components where the keys are the names and the values are the component definition objects. * @param {Object} options Component definition object (a simplified * {@link ng.$compile#directive-definition-object directive definition object}), * with the following properties (all optional): @@ -1252,6 +1253,11 @@ function $CompileProvider($provide, $$sanitizeUriProvider) { * See also {@link ng.$compileProvider#directive $compileProvider.directive()}. */ this.component = function registerComponent(name, options) { + if (!isString(name)) { + forEach(name, reverseParams(bind(this, registerComponent))); + return this; + } + var controller = options.controller || function() {}; function factory($injector) { From 15d1b42829d48aa1d369dbca34d881ad16a39c37 Mon Sep 17 00:00:00 2001 From: Martin Staffa Date: Fri, 30 Jun 2017 15:48:20 +0200 Subject: [PATCH 3/3] improve docs --- src/ng/compile.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ng/compile.js b/src/ng/compile.js index da1e65c56647..44b641bd3f17 100644 --- a/src/ng/compile.js +++ b/src/ng/compile.js @@ -1169,7 +1169,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) { * @ngdoc method * @name $compileProvider#component * @module ng - * @param {string} name Name of the component in camelCase (i.e. `myComp` which will match ``), + * @param {string|Object} name Name of the component in camelCase (i.e. `myComp` which will match ``), * or an object map of components where the keys are the names and the values are the component definition objects. * @param {Object} options Component definition object (a simplified * {@link ng.$compile#directive-definition-object directive definition object}),