我想将父组件的FormGroup传递给它的子组件,以便使用子组件显示错误消息
鉴于以下父母
parent.cmponent.ts
import { Component, OnInit } from '@angular/core'
import {
REACTIVE_FORM_DIRECTIVES, AbstractControl, FormBuilder, FormControl,
FormGroup, Validators
} from '@angular/forms'
@Component(
{
moduleId: module.id,
selector: 'parent-cmp',
templateUrl: 'language.component.html',
styleUrls: ['language.component.css'],
directives: [
ErrorMessagesComponent]
})
export class ParentCmp implements OnInit {
form: FormGroup;
first: AbstractControl;
second: AbstractControl;
constructor(private _fb: FormBuilder) {
this.first =
new FormControl('');
this.second = new FormControl('')
}
ngOnInit() {
this.form = this._fb.group(
{
'first': this.first,
'second': this.second
});
}
}
我现在想将表单:FormGroup变量传递给下面的子组件
error-message.component.ts
import { Component, OnInit, Input } from '@angular/core'
import { NgIf } from '@angular/common'
import {REACTIVE_FORM_DIRECTIVES, FormGroup } from '@angular/forms'
@Component(
{
moduleId: module.id,
selector: 'epimss-error-messages',
template: `
<span class="error" *ngIf="errorMessage !== null">{{errorMessage}}</span>`,
styles: [],
directives: [REACTIVE_FORM_DIRECTIVES, NgIf]
})
export class ErrorMessagesComponent implements OnInit {
@Input()
ctrlName: string
constructor(private _form: FormGroup) {
}
ngOnInit() {
}
get errorMessage() {
// Find the control in the Host (Parent) form
let ctrl = this._form.find(this.ctrlName);
console.log('ctrl| ', ctrl)
// for (let propertyName of ctrl.errors) {
// // If control has a error
// if (ctrl.errors.hasOwnProperty(propertyName) && ctrl.touched) {
// // Return the appropriate error message from the Validation Service
// return CustomValidators.getValidatorErrorMessage(propertyName);
// }
// }
return null;
}
构造函数formGroup表示父级的FormGroup - 以其当前形式不起作用 .
我试图在http://iterity.io/2016/05/01/angular/angular-2-forms-and-advanced-custom-validation/跟随这个过时的例子
3 回答
在父组件中执行此操作
然后在您的子组件中,您可以像这样获得该引用...
您甚至可以通过更改其选择器来确保在组件上指定
formGroupName
或[formGroup]
.这个答案应该足以满足您的需求,但如果您想了解更多,我在这里写了一篇博客文章
https://mrpmorris.blogspot.co.uk/2017/08/angular-composite-controls-formgroup-formgroupname-reactiveforms.html
这是父formGroup中使用的子组件的示例:子组件ts:
css日期选择器:
并像这样使用:
我会将表单作为输入传递给子组件;
当然,您需要将表单从父组件传递给子组件,您可以通过不同方式执行此操作,但最简单的方法是:
在你父母的某个地方;