angular - Angular 5 亲子沟通
问题描述
我想为 Create & Edit 创建组件,但它共享相同的表单,所以我在另一个组件中创建表单。
这是代码:
父级: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'];
});
}
}
}
组织编辑组件.html
<h1>Children </h1>
<app-organization-form [org]="org"></app-organization-form>
子组件: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() {
}
}
组织表单组件.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>
当我运行代码时出现错误:
错误类型错误:无法读取未定义的属性“获取”
它指的是儿童组件html
<form [formGroup]="myForm" (ngSubmit)="save()" class="form-horizontal">
我的路线.ts
{路径:'organisation/edit/:id',组件:OrganizationEditComponent,解析:{organization:OrganizationResolver}},
父组件很好地从解析器获取组织数据。但是子组件无法接收数据?我该怎么办 ?以及如何将“org”数据发送回父级?我应该使用 @ViewChild 吗?
编辑:我通过键入在子组件 html 中测试 org
{{ org | json }}
它工作,但如果我把表格像上面的代码它不起作用。似乎我有来自孩子“createform()”的错误,它无法从 OnInit 中的父级获取组织数据
解决方案
我认为您的表单名称出错了。
您已经编写了 editForm而不是myForm。
例如。
'has-error': editForm.get('organizationName').touched
推荐阅读
- google-app-engine - 通过offline_access 范围获取refresh_token 与IAP 一起使用
- ios - PageViewController 之后的 NavigationView,点击不起作用 - Swiftui
- node.js - 当我尝试使用 nodejs 创建新文件时出现错误
- swift - MapKit 错误:NSHostingView 在渲染其 SwiftUI 内容时被重新布局
- c# - 如何使用 MimeKit/MailKit 设置内容传输编码?
- python - subprocess.Popen 通信方法自动打开文件,停止程序执行,直到我手动关闭文件
- kotlin - 从包含特定子字符串的列表中删除项目?
- oracle - 计算记录数
- javascript - 单击网站的任何部分时,幻灯片会滚动
- java - 我的 WebMvcTest 不会在 IntelliJ 中运行,但会在 Maven 中运行