【发布时间】:2019-03-24 11:32:23
【问题描述】:
我想优化我的应用程序,以免在两个组件中使用相同的验证字段。
我使用字段检查组件"GalleryAddComponent" 中的"title" 和"url"(当我添加一个元素时),并使用相同的字段检查组件"GalleryItemComponent" 中的"title" 和"url"(当我编辑零件)。在组件中,只有表单中的按钮不同,在"GalleryAddComponent"中有一个按钮“add”,它调用添加元素的方法,在组件"GalleryItemComponent"中有一个按钮“updatePost”,它保存了元素的变化.
我了解需要将字段转移到单独的组件并以组件的形式连接:"GalleryAddComponent" 和"GalleryItemComponent"。但是如何正确地保存按钮的验证及其逻辑。帮助请了解如何实现这个想法,以及是否可以在“StackBlitz”中树立榜样。
链接到我的项目结构:StackBlitz
GalleryAddComponent 的模板:
<h3>Add new product</h3>
<div class="card">
<div class="card-body">
<form [formGroup]="angForm" novalidate>
<div class="form-group">
<label class="col-md-4">Picture Title</label>
<input type="text" class="form-control" formControlName="title" minlength="1" #title/>
</div>
<div *ngIf="angForm.controls['title'].invalid && (angForm.controls['title'].dirty || angForm.controls['title'].touched)"
class="alert alert-danger">
<div *ngIf="angForm.controls['title'].errors.required">
Title is required.
</div>
</div>
<div class="form-group">
<label class="col-md-4">Picture Address (url)</label>
<input type="url" class="form-control" formControlName="url" #url pattern="https?://.+"
title="Include http://"/>
</div>
<div *ngIf="angForm.controls['url'].invalid && (angForm.controls['url'].dirty || angForm.controls['url'].touched)"
class="alert alert-danger">
Address(url) is required.
<div *ngIf="angForm.controls['url'].errors.required ">
</div>
</div>
<div class="form-group but-group">
<button (click)="addPost(title.value, url.value); angForm.reset(title.value, url.value)"
[disabled]="angForm.pristine || angForm.invalid"
class="btn btn-primary">Add
</button>
<a routerLink="/gallery" class="btn btn-danger">Back</a>
</div>
</form>
</div>
</div>
GalleryAddComponent 代码:
export class GalleryAddComponent {
angForm: FormGroup;
isAdded: boolean = false;
constructor(private fb: FormBuilder, private galleryService: GalleryService) {
this.createForm();
}
createForm(): void {
this.angForm = this.fb.group({
title: ['', Validators.required],
url: ['', Validators.required]
});
}
addPost(title: string, url: string): void {
this.galleryService.add(title, url).subscribe(res => {
this.isAdded = true;
});
}
}
GalleryItemComponent 的模板:
<h4>Edit your post</h4>
<div class="card-body">
<form [formGroup]="angFormEd" novalidate>
<div class="form-group">
<label class="col-md-4">Picture Title</label>
<input type="text" class="form-control" formControlName="titleEd" #titleEd
/>
</div>
<div *ngIf="angFormEd.controls['titleEd'].invalid && (angFormEd.controls['titleEd'].dirty || angFormEd.controls['titleEd'].touched)"
class="alert alert-danger">
<div *ngIf="angFormEd.controls['titleEd'].errors.required">
Title is required.
</div>
</div>
<div class="form-group">
<label class="col-md-4">Picture Address (url)</label>
<input type="url" class="form-control" formControlName="urlEd" #urlEd pattern="https?://.+"
title="Include http://"/>
</div>
<div *ngIf="angFormEd.controls['urlEd'].invalid && (angFormEd.controls['urlEd'].dirty || angFormEd.controls['urlEd'].touched)"
class="alert alert-danger">
Address(url) is required.
<div *ngIf="angFormEd.controls['urlEd'].errors.required ">
</div>
</div>
<div class="form-group but-group">
<input type="button"
(click)="updatePost(titleEd.value, urlEd.value)"
[disabled]=" angFormEd.invalid"
class="btn btn-primary" value="Update Post">
</div>
</form>
</div>
GalleryItemComponent 的代码:
export class GalleryItemComponent implements OnInit {
pic: Picture;
angFormEd: FormGroup;
constructor( private route: ActivatedRoute, private galleryService: GalleryService, private fb: FormBuilder,) {
}
ngOnInit() {
this.createFormEd();
this.showPost();
}
createFormEd(): void {
this.angFormEd = this.fb.group({
titleEd: ['', Validators.required],
urlEd: ['', Validators.required]
});
}
showPost(): void {
this.route.data.subscribe(params => {
this.pic = params.post;
this.angFormEd.setValue({titleEd: params.post.title, urlEd: params.post.url});
})
}
updatePost(title: string, url: string): void {
this.route.params.subscribe(params => {
this.galleryService.update(title, url, params['id']).subscribe(res => {
if (res.id === this.pic.id) {
this.pic.title = title;
this.pic.url = url;
}
});
});
}
}
【问题讨论】:
标签: javascript html angular typescript angular-components