/** * @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 {BaseException, Directive, Host, Inject, Input, OnDestroy, OnInit, Optional, Self, SkipSelf, forwardRef} from '@angular/core'; import {NG_ASYNC_VALIDATORS, NG_VALIDATORS} from '../validators'; import {AbstractFormGroupDirective} from './abstract_form_group_directive'; import {ControlContainer} from './control_container'; import {NgForm} from './ng_form'; import {TemplateDrivenErrors} from './template_driven_errors'; export const modelGroupProvider: any = { provide: ControlContainer, useExisting: forwardRef(() => NgModelGroup) }; /** * Creates and binds a model group to a DOM element. * * This directive can only be used as a child of {@link NgForm}. * * ```typescript * @Component({ * selector: 'my-app', * template: ` *
*

Angular forms Example

*
*
*

Enter your name:

*

First:

*

Middle:

*

Last:

*
*

Name value:

*
{{ mgName | json }}
*

Name is {{mgName?.valid ? "valid" : "invalid"}}

*

What's your favorite food?

*

*

Form value

*
{{ f | json }}
*
*
* ` * }) * export class App {} * ``` * * This example declares a model group for a user's name. The value and validation state of * this group can be accessed separately from the overall form. * * @experimental */ @Directive({selector: '[ngModelGroup]', providers: [modelGroupProvider], exportAs: 'ngModelGroup'}) export class NgModelGroup extends AbstractFormGroupDirective implements OnInit, OnDestroy { @Input('ngModelGroup') name: string; constructor( @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; } /** @internal */ _checkParentType(): void { if (!(this._parent instanceof NgModelGroup) && !(this._parent instanceof NgForm)) { TemplateDrivenErrors.modelGroupParentException(); } } }