這是一個帶有雙向綁定輸入字段的窗體。目標是創建相同的表單來編輯和創建數據。這已經完成了,但我確信可以有更好的方法來做到這一點(如使用AbstractControl功能)。我也錯過了這裏的一個修復 - 如果clickEdit()
被點擊需要更新用戶想要編輯的對象的表單值。感謝您的幫助和有關AbstractControl
和NgModel
特別說明..用於創建和編輯數據的相同形式Angular4
<div>
<form (ngSubmit)="clicked ? onEditSubmit($event) : onRegisterSubmit($event)" [formGroup] = "form">
<div class="form-group">
<label>Full Name</label>
<input type="text" [(ngModel)]="fullname" formControlName="fullname" class="form-control" >
</div>
<div class="form-group">
<label>Username</label>
<input type="text" [(ngModel)]="username" formControlName="username" class="form-control" >
</div>
<div class="form-group">
<label>Email</label>
<input type="text" [(ngModel)]="email" formControlName="email" class="form-control" >
</div>
<div class="form-group">
<label>Password</label>
<input type="password" [(ngModel)]="password" formControlName="password" class="form-control">
</div>
<button type="submit" class="btn btn-primary" [disabled]="!form.valid"> Submit </button>
</form>
</div>
<br>
<br>
<table border="2" class="table table-striped">
<tr>
<th>Full Name</th>
<th>Username</th>
<th>Email</th>
<th>Password</th>
<th>Delete</th>
<th>Edit</th>
</tr>
<div > </div>
<tr *ngFor="let user of userDetails; index as i">
<td>{{user.fullname}}</td>
<td>{{user.username}}</td>
<td>{{user.email}}</td>
<td>{{user.password}}</td>
<td><button (click)="userDelete()">X</button></td>
<td><button (click)="clickEdit(i)">Edit</button></td>
</tr>
</table>
而且
import { Component } from '@angular/core';
import { initializeApp, database } from 'firebase';
import { FormControl, FormGroup, Validators } from '@angular/forms';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
})
export class AppComponent {
fullname : string;
username : string;
email : string;
password : string;
clicked = false;
userDetails:Array<object>;
form;
ngOnInit() {
this.userDetails=[];
this.form = new FormGroup({
fullname : new FormControl("", Validators.required),
username : new FormControl("", Validators.required),
email : new FormControl("", Validators.required),
password : new FormControl("", Validators.required)
});
}
onRegisterSubmit(e){
let user = {
fullname : this.fullname ,
username : this.username,
email : this.email ,
password : this.password
}
this.userDetails.push(user);
this.clearInputFields(e);
}
editIndex = null;
clickEdit(i){
this.clicked = !this.clicked;
this.editIndex = i;
}
onEditSubmit(e) {
let editUser = {
fullname : this.fullname ,
username : this.username,
email : this.email ,
password : this.password
}
this.userDetails[this.editIndex] = editUser;
this.clearInputFields(e);
this.clicked = !this.clicked;
}
clearInputFields(e){
let all = e.target.querySelectorAll('input');
Object.keys(all).forEach(key => {
console.log(all[key].value = '');
});
}
}
非常感謝您的詳細解答。我沒有從頭開始製作這個表單,我只是實現了一些功能,是的,我也認爲這裏不需要ngModel。但你的答案看起來不錯,我會詳細探討它。乾杯! –
當然,讓我知道如果您遇到任何問題,我很樂意提供幫助。我測試了上面的代碼,所以它肯定應該工作:) – Alex