/** * @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 {AfterViewInit, Directive, EventEmitter, Inject, Input, Optional, Self, forwardRef} from '@angular/core'; import {AbstractControl, FormControl, FormGroup, FormHooks} from '../model'; import {NG_ASYNC_VALIDATORS, NG_VALIDATORS} from '../validators'; import {ControlContainer} from './control_container'; import {Form} from './form_interface'; import {NgControl} from './ng_control'; import {NgModel} from './ng_model'; import {NgModelGroup} from './ng_model_group'; import {composeAsyncValidators, composeValidators, removeDir, setUpControl, setUpFormContainer, syncPendingControls} from './shared'; export const formDirectiveProvider: any = { provide: ControlContainer, useExisting: forwardRef(() => NgForm) }; const resolvedPromise = Promise.resolve(null); /** * @description * * Creates a top-level `FormGroup` instance and binds it to a form * to track aggregate form value and validation status. * * As soon as you import the `FormsModule`, this directive becomes active by default on * all `
* ``` * */ @Input('ngFormOptions') options: {updateOn?: FormHooks}; constructor( @Optional() @Self() @Inject(NG_VALIDATORS) validators: any[], @Optional() @Self() @Inject(NG_ASYNC_VALIDATORS) asyncValidators: any[]) { super(); this.form = new FormGroup({}, composeValidators(validators), composeAsyncValidators(asyncValidators)); } ngAfterViewInit() { this._setUpdateStrategy(); } get formDirective(): Form { return this; } get control(): FormGroup { return this.form; } get path(): string[] { return []; } get controls(): {[key: string]: AbstractControl} { return this.form.controls; } addControl(dir: NgModel): void { resolvedPromise.then(() => { const container = this._findContainer(dir.path); (dir as{control: FormControl}).control =