File tree Expand file tree Collapse file tree 3 files changed +29
-4
lines changed Expand file tree Collapse file tree 3 files changed +29
-4
lines changed Original file line number Diff line number Diff line change @@ -259,7 +259,7 @@ export class FormArray<TControl extends AbstractControl<any> = any> extends Abst
259
259
onlySelf? : boolean ;
260
260
emitEvent? : boolean ;
261
261
}): void ;
262
- push(control : TControl , options ? : {
262
+ push(control : TControl | Array < TControl > , options ? : {
263
263
emitEvent? : boolean ;
264
264
}): void ;
265
265
removeAt(index : number , options ? : {
Original file line number Diff line number Diff line change @@ -174,9 +174,16 @@ export class FormArray<TControl extends AbstractControl<any> = any> extends Abst
174
174
* `valueChanges` observables emit events with the latest status and value when the control is
175
175
* inserted. When false, no events are emitted.
176
176
*/
177
- push ( control : TControl , options : { emitEvent ?: boolean } = { } ) : void {
178
- this . controls . push ( control ) ;
179
- this . _registerControl ( control ) ;
177
+ push ( control : TControl | Array < TControl > , options : { emitEvent ?: boolean } = { } ) : void {
178
+ if ( Array . isArray ( control ) ) {
179
+ control . forEach ( ( ctrl ) => {
180
+ this . controls . push ( ctrl ) ;
181
+ this . _registerControl ( ctrl ) ;
182
+ } ) ;
183
+ } else {
184
+ this . controls . push ( control ) ;
185
+ this . _registerControl ( control ) ;
186
+ }
180
187
this . updateValueAndValidity ( { emitEvent : options . emitEvent } ) ;
181
188
this . _onCollectionChange ( ) ;
182
189
}
Original file line number Diff line number Diff line change @@ -41,6 +41,12 @@ import {asyncValidator} from './util';
41
41
expect ( a . controls ) . toEqual ( [ c1 ] ) ;
42
42
} ) ;
43
43
44
+ it ( 'should support pushing an array' , ( ) => {
45
+ a . push ( [ c1 , c2 ] ) ;
46
+ expect ( a . length ) . toEqual ( 2 ) ;
47
+ expect ( a . controls ) . toEqual ( [ c1 , c2 ] ) ;
48
+ } ) ;
49
+
44
50
it ( 'should support removing' , ( ) => {
45
51
a . push ( c1 ) ;
46
52
a . push ( c2 ) ;
@@ -971,6 +977,18 @@ import {asyncValidator} from './util';
971
977
972
978
a . push ( c2 ) ;
973
979
} ) ;
980
+
981
+ it ( 'should fire an event once when calling `FormArray.push` with an array of controls' , ( done ) => {
982
+ a = new FormArray < any > ( [ ] ) ;
983
+ a . valueChanges . subscribe ( {
984
+ next : ( value : any ) => {
985
+ expect ( value ) . toEqual ( [ 'old1' , 'old2' ] ) ;
986
+ done ( ) ;
987
+ } ,
988
+ } ) ;
989
+
990
+ a . push ( [ c1 , c2 ] ) ;
991
+ } ) ;
974
992
} ) ;
975
993
976
994
describe ( 'get' , ( ) => {
You can’t perform that action at this time.
0 commit comments