Angular 5 Parent 和 Child 通信
Angular 5 Parent and Child Communication
我想为创建和编辑创建组件,但它共享相同的表单,所以我在另一个组件中创建表单。
代码如下:
Parent:OrganizationEditComponent(我使用解析器填充组织数据)
import { Component, OnInit, Input, EventEmitter, Output } from '@angular/core';
import { FormGroup, FormControl, Validators, FormBuilder } from '@angular/forms';
import { Router, ActivatedRoute } from '@angular/router';
import { AlertifyService } from './../../_services/alertify.service';
import { Organization } from './../../_models/organization';
import { OrganizationService } from './../../_services/organization.service';
@Component({
selector: 'app-organization-edit',
templateUrl: './organization-edit.component.html',
styleUrls: ['./organization-edit.component.css']
})
export class OrganizationEditComponent implements OnInit {
org: Organization;
editForm: FormGroup;
constructor(private orgService: OrganizationService,
private fb: FormBuilder,
private route: ActivatedRoute,
private router: Router,
private alertify: AlertifyService) { }
ngOnInit() {
this.route.data.subscribe( data => {
this.org = data['organization'];
});
}
}
}
OrganizationEditComponent.html
<h1>Children </h1>
<app-organization-form [org]="org"></app-organization-form>
Children分量:OrganizationFormComponent.ts
import { Component, OnInit, Input, EventEmitter, Output } from '@angular/core';
import { FormGroup, FormControl, Validators, FormBuilder } from '@angular/forms';
import { Router, ActivatedRoute } from '@angular/router';
import { AlertifyService } from './../../_services/alertify.service';
import { Organization } from './../../_models/organization';
import { OrganizationService } from './../../_services/organization.service';
@Component({
selector: 'app-organization-form',
templateUrl: './organization-form.component.html',
styleUrls: ['./organization-form.component.css']
})
export class OrganizationFormComponent implements OnInit {
@Input() org: Organization;
myForm: FormGroup;
constructor(private orgService: OrganizationService,
private fb: FormBuilder,
private route: ActivatedRoute,
private router: Router,
private alertify: AlertifyService
) { }
ngOnInit() {
this.createForm();
}
createForm() {
this.myForm = this.fb.group({
organizationName: [this.org.organizationName, Validators.required],
legalName: [this.org.legalName, Validators.required],
logoUrl: [this.org.logoUrl, Validators.required],
abn: [this.org.abn, Validators.required],
acn: [this.org.acn, Validators.required]
});
}
save() {
}
}
OrganizationFormComponent.html
{{org |json}}
<div class="container">
<div class="row">
<form [formGroup]="myForm" (ngSubmit)="save()" class="form-horizontal">
<div class="form-group" [ngClass]="{'has-error': editForm.get('organizationName').touched && editForm.get('organizationName').hasError('required')}">
<label class="col-sm-2" for="organizationName">organizationName</label>
<div class="col-sm-7">
<input class="form-control" placeholder="organizationName" formControlName="organizationName">
<span class="help-block" *ngIf="editForm.get('organizationName').touched && editForm.get('organizationName').hasError('required')">organizationName is required</span>
</div>
</div>
<div class="form-group" [ngClass]="{'has-error': editForm.get('legalName').touched && editForm.get('legalName').hasError('required')}">
<label class="col-sm-2" for="legalName">legalName</label>
<div class="col-sm-7">
<input class="form-control" placeholder="legalName" formControlName="legalName">
<span class="help-block" *ngIf="editForm.get('legalName').touched && editForm.get('legalName').hasError('required')">legalName is required</span>
</div>
</div>
<div class="form-group" [ngClass]="{'has-error': editForm.get('logoUrl').touched && editForm.get('logoUrl').hasError('required')}">
<label class="col-sm-2" for="logoUrl">logoUrl</label>
<div class="col-sm-7">
<input class="form-control" placeholder="logoUrl" formControlName="logoUrl">
<span class="help-block" *ngIf="editForm.get('logoUrl').touched && editForm.get('logoUrl').hasError('required')">logoUrl is required</span>
</div>
</div>
<div class="form-group" [ngClass]="{'has-error': editForm.get('abn').touched && editForm.get('abn').hasError('required')}">
<label class="col-sm-2" for="abn">abn</label>
<div class="col-sm-7">
<input class="form-control" placeholder="abn" formControlName="abn">
<span class="help-block" *ngIf="editForm.get('abn').touched && editForm.get('abn').hasError('required')">abn is required</span>
</div>
</div>
<div class="form-group" [ngClass]="{'has-error': editForm.get('acn').touched && editForm.get('acn').hasError('required')}">
<label class="col-sm-2" for="acn">acn</label>
<div class="col-sm-7">
<input class="form-control" placeholder="acn" formControlName="acn">
<span class="help-block" *ngIf="editForm.get('acn').touched && editForm.get('acn').hasError('required')">acn is required</span>
</div>
</div>
</form>
</div>
</div>
当我 运行 代码时出现错误:
ERROR TypeError: Cannot read property 'get' of undefined
它指的是子组件 html
<form [formGroup]="myForm" (ngSubmit)="save()" class="form-horizontal">
我的routes.ts
{路径:'organisation/edit/:id',组件:OrganizationEditComponent,解析:{组织:OrganizationResolver}},
parent 组件可以很好地从解析器获取组织数据。但是 child 组件无法接收数据?我应该怎么办 ?
以及如何将 "org" 数据发送回 parent ?我应该使用 @ViewChild 吗?
编辑:
我通过输入
在 child 组件 html 中测试组织
{{ org | json }}
它可以工作,但是如果我像上面的代码那样放置表格,它就没有工作。看来我在 children "createform()" 中遇到错误,它无法从 OnInit
中的 parent 获取组织数据
我认为您输入的表单名称有误。
您写的是 editForm 而不是 myForm。
例如
'has-error': editForm.get('organizationName').touched
我想为创建和编辑创建组件,但它共享相同的表单,所以我在另一个组件中创建表单。
代码如下:
Parent:OrganizationEditComponent(我使用解析器填充组织数据)
import { Component, OnInit, Input, EventEmitter, Output } from '@angular/core';
import { FormGroup, FormControl, Validators, FormBuilder } from '@angular/forms';
import { Router, ActivatedRoute } from '@angular/router';
import { AlertifyService } from './../../_services/alertify.service';
import { Organization } from './../../_models/organization';
import { OrganizationService } from './../../_services/organization.service';
@Component({
selector: 'app-organization-edit',
templateUrl: './organization-edit.component.html',
styleUrls: ['./organization-edit.component.css']
})
export class OrganizationEditComponent implements OnInit {
org: Organization;
editForm: FormGroup;
constructor(private orgService: OrganizationService,
private fb: FormBuilder,
private route: ActivatedRoute,
private router: Router,
private alertify: AlertifyService) { }
ngOnInit() {
this.route.data.subscribe( data => {
this.org = data['organization'];
});
}
}
}
OrganizationEditComponent.html
<h1>Children </h1>
<app-organization-form [org]="org"></app-organization-form>
Children分量:OrganizationFormComponent.ts
import { Component, OnInit, Input, EventEmitter, Output } from '@angular/core';
import { FormGroup, FormControl, Validators, FormBuilder } from '@angular/forms';
import { Router, ActivatedRoute } from '@angular/router';
import { AlertifyService } from './../../_services/alertify.service';
import { Organization } from './../../_models/organization';
import { OrganizationService } from './../../_services/organization.service';
@Component({
selector: 'app-organization-form',
templateUrl: './organization-form.component.html',
styleUrls: ['./organization-form.component.css']
})
export class OrganizationFormComponent implements OnInit {
@Input() org: Organization;
myForm: FormGroup;
constructor(private orgService: OrganizationService,
private fb: FormBuilder,
private route: ActivatedRoute,
private router: Router,
private alertify: AlertifyService
) { }
ngOnInit() {
this.createForm();
}
createForm() {
this.myForm = this.fb.group({
organizationName: [this.org.organizationName, Validators.required],
legalName: [this.org.legalName, Validators.required],
logoUrl: [this.org.logoUrl, Validators.required],
abn: [this.org.abn, Validators.required],
acn: [this.org.acn, Validators.required]
});
}
save() {
}
}
OrganizationFormComponent.html
{{org |json}}
<div class="container">
<div class="row">
<form [formGroup]="myForm" (ngSubmit)="save()" class="form-horizontal">
<div class="form-group" [ngClass]="{'has-error': editForm.get('organizationName').touched && editForm.get('organizationName').hasError('required')}">
<label class="col-sm-2" for="organizationName">organizationName</label>
<div class="col-sm-7">
<input class="form-control" placeholder="organizationName" formControlName="organizationName">
<span class="help-block" *ngIf="editForm.get('organizationName').touched && editForm.get('organizationName').hasError('required')">organizationName is required</span>
</div>
</div>
<div class="form-group" [ngClass]="{'has-error': editForm.get('legalName').touched && editForm.get('legalName').hasError('required')}">
<label class="col-sm-2" for="legalName">legalName</label>
<div class="col-sm-7">
<input class="form-control" placeholder="legalName" formControlName="legalName">
<span class="help-block" *ngIf="editForm.get('legalName').touched && editForm.get('legalName').hasError('required')">legalName is required</span>
</div>
</div>
<div class="form-group" [ngClass]="{'has-error': editForm.get('logoUrl').touched && editForm.get('logoUrl').hasError('required')}">
<label class="col-sm-2" for="logoUrl">logoUrl</label>
<div class="col-sm-7">
<input class="form-control" placeholder="logoUrl" formControlName="logoUrl">
<span class="help-block" *ngIf="editForm.get('logoUrl').touched && editForm.get('logoUrl').hasError('required')">logoUrl is required</span>
</div>
</div>
<div class="form-group" [ngClass]="{'has-error': editForm.get('abn').touched && editForm.get('abn').hasError('required')}">
<label class="col-sm-2" for="abn">abn</label>
<div class="col-sm-7">
<input class="form-control" placeholder="abn" formControlName="abn">
<span class="help-block" *ngIf="editForm.get('abn').touched && editForm.get('abn').hasError('required')">abn is required</span>
</div>
</div>
<div class="form-group" [ngClass]="{'has-error': editForm.get('acn').touched && editForm.get('acn').hasError('required')}">
<label class="col-sm-2" for="acn">acn</label>
<div class="col-sm-7">
<input class="form-control" placeholder="acn" formControlName="acn">
<span class="help-block" *ngIf="editForm.get('acn').touched && editForm.get('acn').hasError('required')">acn is required</span>
</div>
</div>
</form>
</div>
</div>
当我 运行 代码时出现错误:
ERROR TypeError: Cannot read property 'get' of undefined
它指的是子组件 html
<form [formGroup]="myForm" (ngSubmit)="save()" class="form-horizontal">
我的routes.ts
{路径:'organisation/edit/:id',组件:OrganizationEditComponent,解析:{组织:OrganizationResolver}},
parent 组件可以很好地从解析器获取组织数据。但是 child 组件无法接收数据?我应该怎么办 ? 以及如何将 "org" 数据发送回 parent ?我应该使用 @ViewChild 吗?
编辑: 我通过输入
在 child 组件 html 中测试组织{{ org | json }}
它可以工作,但是如果我像上面的代码那样放置表格,它就没有工作。看来我在 children "createform()" 中遇到错误,它无法从 OnInit
中的 parent 获取组织数据我认为您输入的表单名称有误。
您写的是 editForm 而不是 myForm。
例如
'has-error': editForm.get('organizationName').touched