69 lines
2.4 KiB
TypeScript
69 lines
2.4 KiB
TypeScript
/**
|
|
* @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 {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)
|
|
};
|
|
|
|
/**
|
|
* @whatItDoes Creates and binds a {@link FormGroup} instance to a DOM element.
|
|
*
|
|
* @howToUse
|
|
*
|
|
* This directive can only be used as a child of {@link NgForm} (or in other words,
|
|
* within `<form>` tags).
|
|
*
|
|
* Use this directive if you'd like to create a sub-group within a form. This can
|
|
* come in handy if you want to validate a sub-group of your form separately from
|
|
* the rest of your form, or if some values in your domain model make more sense to
|
|
* consume together in a nested object.
|
|
*
|
|
* Pass in the name you'd like this sub-group to have and it will become the key
|
|
* for the sub-group in the form's full value. You can also export the directive into
|
|
* a local template variable using `ngModelGroup` (ex: `#myGroup="ngModelGroup"`).
|
|
*
|
|
* {@example forms/ts/ngModelGroup/ng_model_group_example.ts region='Component'}
|
|
*
|
|
* * **npm package**: `@angular/forms`
|
|
*
|
|
* * **NgModule**: `FormsModule`
|
|
*
|
|
* @stable
|
|
*/
|
|
@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();
|
|
}
|
|
}
|
|
}
|