@ -1,107 +0,0 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {Directive, Host, Inject, Input, OnDestroy, OnInit, Optional, Self, SkipSelf, forwardRef} from '@angular/core';
|
||||
|
||||
import {BaseException} from '../../facade/exceptions';
|
||||
import {FormArray} from '../../model';
|
||||
import {NG_ASYNC_VALIDATORS, NG_VALIDATORS} from '../../validators';
|
||||
import {ControlContainer} from '../control_container';
|
||||
import {ReactiveErrors} from '../reactive_errors';
|
||||
import {composeAsyncValidators, composeValidators, controlPath} from '../shared';
|
||||
import {AsyncValidatorFn, ValidatorFn} from '../validators';
|
||||
|
||||
import {FormGroupDirective} from './form_group_directive';
|
||||
import {FormGroupName} from './form_group_name';
|
||||
|
||||
export const formArrayNameProvider: any = {
|
||||
provide: ControlContainer,
|
||||
useExisting: forwardRef(() => FormArrayName)
|
||||
};
|
||||
|
||||
/**
|
||||
* Syncs an existing form array to a DOM element.
|
||||
*
|
||||
* This directive can only be used as a child of {@link FormGroupDirective}.
|
||||
*
|
||||
* ```typescript
|
||||
* @Component({
|
||||
* selector: 'my-app',
|
||||
* template: `
|
||||
* <div>
|
||||
* <h2>Angular FormArray Example</h2>
|
||||
* <form [formGroup]="myForm">
|
||||
* <div formArrayName="cities">
|
||||
* <div *ngFor="let city of cityArray.controls; let i=index">
|
||||
* <input [formControlName]="i">
|
||||
* </div>
|
||||
* </div>
|
||||
* </form>
|
||||
* {{ myForm.value | json }} // {cities: ['SF', 'NY']}
|
||||
* </div>
|
||||
* `
|
||||
* })
|
||||
* export class App {
|
||||
* cityArray = new FormArray([
|
||||
* new FormControl('SF'),
|
||||
* new FormControl('NY')
|
||||
* ]);
|
||||
* myForm = new FormGroup({
|
||||
* cities: this.cityArray
|
||||
* });
|
||||
* }
|
||||
* ```
|
||||
*
|
||||
* @experimental
|
||||
*/
|
||||
@Directive({selector: '[formArrayName]', providers: [formArrayNameProvider]})
|
||||
export class FormArrayName extends ControlContainer implements OnInit, OnDestroy {
|
||||
/** @internal */
|
||||
_parent: ControlContainer;
|
||||
|
||||
/** @internal */
|
||||
_validators: any[];
|
||||
|
||||
/** @internal */
|
||||
_asyncValidators: any[];
|
||||
|
||||
@Input('formArrayName') name: string;
|
||||
|
||||
constructor(
|
||||
@Optional() @Host() @SkipSelf() parent: ControlContainer,
|
||||
@Optional() @Self() @Inject(NG_VALIDATORS) validators: any[],
|
||||
@Optional() @Self() @Inject(NG_ASYNC_VALIDATORS) asyncValidators: any[]) {
|
||||
super();
|
||||
this._parent = parent;
|
||||
this._validators = validators;
|
||||
this._asyncValidators = asyncValidators;
|
||||
}
|
||||
|
||||
ngOnInit(): void {
|
||||
this._checkParentType();
|
||||
this.formDirective.addFormArray(this);
|
||||
}
|
||||
|
||||
ngOnDestroy(): void { this.formDirective.removeFormArray(this); }
|
||||
|
||||
get control(): FormArray { return this.formDirective.getFormArray(this); }
|
||||
|
||||
get formDirective(): FormGroupDirective { return <FormGroupDirective>this._parent.formDirective; }
|
||||
|
||||
get path(): string[] { return controlPath(this.name, this._parent); }
|
||||
|
||||
get validator(): ValidatorFn { return composeValidators(this._validators); }
|
||||
|
||||
get asyncValidator(): AsyncValidatorFn { return composeAsyncValidators(this._asyncValidators); }
|
||||
|
||||
private _checkParentType(): void {
|
||||
if (!(this._parent instanceof FormGroupName) && !(this._parent instanceof FormGroupDirective)) {
|
||||
ReactiveErrors.arrayParentException();
|
||||
}
|
||||
}
|
||||
}
|
@ -9,7 +9,6 @@
|
||||
import {Directive, Host, Inject, Input, OnChanges, OnDestroy, Optional, Output, Self, SimpleChanges, SkipSelf, forwardRef} from '@angular/core';
|
||||
|
||||
import {EventEmitter, ObservableWrapper} from '../../facade/async';
|
||||
import {BaseException} from '../../facade/exceptions';
|
||||
import {FormControl} from '../../model';
|
||||
import {NG_ASYNC_VALIDATORS, NG_VALIDATORS} from '../../validators';
|
||||
import {AbstractFormGroupDirective} from '../abstract_form_group_directive';
|
||||
@ -20,10 +19,8 @@ import {ReactiveErrors} from '../reactive_errors';
|
||||
import {composeAsyncValidators, composeValidators, controlPath, isPropertyUpdated, selectValueAccessor} from '../shared';
|
||||
import {AsyncValidatorFn, ValidatorFn} from '../validators';
|
||||
|
||||
import {FormArrayName} from './form_array_name';
|
||||
import {FormGroupDirective} from './form_group_directive';
|
||||
import {FormGroupName} from './form_group_name';
|
||||
|
||||
import {FormArrayName, FormGroupName} from './form_group_name';
|
||||
|
||||
export const controlNameBinding: any = {
|
||||
provide: NgControl,
|
||||
|
@ -20,8 +20,7 @@ import {NgControl} from '../ng_control';
|
||||
import {ReactiveErrors} from '../reactive_errors';
|
||||
import {composeAsyncValidators, composeValidators, setUpControl, setUpFormContainer} from '../shared';
|
||||
|
||||
import {FormArrayName} from './form_array_name';
|
||||
import {FormGroupName} from './form_group_name';
|
||||
import {FormArrayName, FormGroupName} from './form_group_name';
|
||||
|
||||
export const formDirectiveProvider: any = {
|
||||
provide: ControlContainer,
|
||||
|
@ -8,11 +8,13 @@
|
||||
|
||||
import {Directive, Host, Inject, Input, OnDestroy, OnInit, Optional, Self, SkipSelf, forwardRef} from '@angular/core';
|
||||
|
||||
import {BaseException} from '../../facade/exceptions';
|
||||
import {FormArray} from '../../model';
|
||||
import {NG_ASYNC_VALIDATORS, NG_VALIDATORS} from '../../validators';
|
||||
import {AbstractFormGroupDirective} from '../abstract_form_group_directive';
|
||||
import {ControlContainer} from '../control_container';
|
||||
import {ReactiveErrors} from '../reactive_errors';
|
||||
import {composeAsyncValidators, composeValidators, controlPath} from '../shared';
|
||||
import {AsyncValidatorFn, ValidatorFn} from '../validators';
|
||||
|
||||
import {FormGroupDirective} from './form_group_directive';
|
||||
|
||||
@ -85,8 +87,100 @@ export class FormGroupName extends AbstractFormGroupDirective implements OnInit,
|
||||
|
||||
/** @internal */
|
||||
_checkParentType(): void {
|
||||
if (!(this._parent instanceof FormGroupName) && !(this._parent instanceof FormGroupDirective)) {
|
||||
if (_hasInvalidParent(this._parent)) {
|
||||
ReactiveErrors.groupParentException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const formArrayNameProvider: any = {
|
||||
provide: ControlContainer,
|
||||
useExisting: forwardRef(() => FormArrayName)
|
||||
};
|
||||
|
||||
/**
|
||||
* Syncs an existing form array to a DOM element.
|
||||
*
|
||||
* This directive can only be used as a child of {@link FormGroupDirective}.
|
||||
*
|
||||
* ```typescript
|
||||
* @Component({
|
||||
* selector: 'my-app',
|
||||
* template: `
|
||||
* <div>
|
||||
* <h2>Angular FormArray Example</h2>
|
||||
* <form [formGroup]="myForm">
|
||||
* <div formArrayName="cities">
|
||||
* <div *ngFor="let city of cityArray.controls; let i=index">
|
||||
* <input [formControlName]="i">
|
||||
* </div>
|
||||
* </div>
|
||||
* </form>
|
||||
* {{ myForm.value | json }} // {cities: ['SF', 'NY']}
|
||||
* </div>
|
||||
* `
|
||||
* })
|
||||
* export class App {
|
||||
* cityArray = new FormArray([
|
||||
* new FormControl('SF'),
|
||||
* new FormControl('NY')
|
||||
* ]);
|
||||
* myForm = new FormGroup({
|
||||
* cities: this.cityArray
|
||||
* });
|
||||
* }
|
||||
* ```
|
||||
*
|
||||
* @experimental
|
||||
*/
|
||||
@Directive({selector: '[formArrayName]', providers: [formArrayNameProvider]})
|
||||
export class FormArrayName extends ControlContainer implements OnInit, OnDestroy {
|
||||
/** @internal */
|
||||
_parent: ControlContainer;
|
||||
|
||||
/** @internal */
|
||||
_validators: any[];
|
||||
|
||||
/** @internal */
|
||||
_asyncValidators: any[];
|
||||
|
||||
@Input('formArrayName') name: string;
|
||||
|
||||
constructor(
|
||||
@Optional() @Host() @SkipSelf() parent: ControlContainer,
|
||||
@Optional() @Self() @Inject(NG_VALIDATORS) validators: any[],
|
||||
@Optional() @Self() @Inject(NG_ASYNC_VALIDATORS) asyncValidators: any[]) {
|
||||
super();
|
||||
this._parent = parent;
|
||||
this._validators = validators;
|
||||
this._asyncValidators = asyncValidators;
|
||||
}
|
||||
|
||||
ngOnInit(): void {
|
||||
this._checkParentType();
|
||||
this.formDirective.addFormArray(this);
|
||||
}
|
||||
|
||||
ngOnDestroy(): void { this.formDirective.removeFormArray(this); }
|
||||
|
||||
get control(): FormArray { return this.formDirective.getFormArray(this); }
|
||||
|
||||
get formDirective(): FormGroupDirective { return <FormGroupDirective>this._parent.formDirective; }
|
||||
|
||||
get path(): string[] { return controlPath(this.name, this._parent); }
|
||||
|
||||
get validator(): ValidatorFn { return composeValidators(this._validators); }
|
||||
|
||||
get asyncValidator(): AsyncValidatorFn { return composeAsyncValidators(this._asyncValidators); }
|
||||
|
||||
private _checkParentType(): void {
|
||||
if (_hasInvalidParent(this._parent)) {
|
||||
ReactiveErrors.arrayParentException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function _hasInvalidParent(parent: ControlContainer): boolean {
|
||||
return !(parent instanceof FormGroupName) && !(parent instanceof FormGroupDirective) &&
|
||||
!(parent instanceof FormArrayName);
|
||||
}
|
||||
|
Reference in New Issue
Block a user