admin管理员组文章数量:1402990
So I'm trying out some Angular 2 and I like it so far. But I need some help to navigate this new landscape.
I have a form to edit a users details and a list on the side with all my users. When I click on one of the users in the list I want to populate my edit-user-form with the user details (setEditForm(user)).
I've got it working and all. But I must say, it doesn't feel quite right to use ngControl and ngModel at the same time. But maybe it is...
Is this the correct way to do this or have I just got some luck in making it work?
@Component({
template: `
<form (ngSubmit)="editUser(f.value)" #f="ngForm">
<input ngControl="nameInp" [ngModel]="selectedUser.name" type="text">
<input ngControl="ageInp" [ngModel]="selectedUser.age" type="text">
<input ngControl="cityInp" [ngModel]="selectedUser.city" type="text">
<button type="submit">Save</button>
</form>
)}
export class AdminComponent {
selectedUser:UserModel;
constructor() {
this.selectedUser = new UserModel;
}
setEditForm(user:UserModel) {
this.selectedUser = user;
}
editUser(form:any) {
// Update DB with values
console.log(form['nameInp']);
console.log(form['ageInp']);
console.log(form['cityInp']);
}
}
So I'm trying out some Angular 2 and I like it so far. But I need some help to navigate this new landscape.
I have a form to edit a users details and a list on the side with all my users. When I click on one of the users in the list I want to populate my edit-user-form with the user details (setEditForm(user)).
I've got it working and all. But I must say, it doesn't feel quite right to use ngControl and ngModel at the same time. But maybe it is...
Is this the correct way to do this or have I just got some luck in making it work?
@Component({
template: `
<form (ngSubmit)="editUser(f.value)" #f="ngForm">
<input ngControl="nameInp" [ngModel]="selectedUser.name" type="text">
<input ngControl="ageInp" [ngModel]="selectedUser.age" type="text">
<input ngControl="cityInp" [ngModel]="selectedUser.city" type="text">
<button type="submit">Save</button>
</form>
)}
export class AdminComponent {
selectedUser:UserModel;
constructor() {
this.selectedUser = new UserModel;
}
setEditForm(user:UserModel) {
this.selectedUser = user;
}
editUser(form:any) {
// Update DB with values
console.log(form['nameInp']);
console.log(form['ageInp']);
console.log(form['cityInp']);
}
}
Share
Improve this question
asked Mar 11, 2016 at 20:57
mottossonmottosson
3,7635 gold badges39 silver badges81 bronze badges
1 Answer
Reset to default 13Sure you can use ngControl
/ ngFormControl
and ngModel
at the same time. From the Angular2 documentation (https://angular.io/docs/ts/latest/guide/forms.html):
two-way data binding with [(ngModel)] syntax for reading and writing values to input controls
using ngControl to track the change state and validity of form controls
displaying validation errors to users and enable/disable form controls
sharing information among controls with template local variables
In short, I would use ngModel
if I need two-way binding and ngForm
/ ngFormControl
if I need validation but you can mix both.
If you only need to get values and notifications when input values are updated, ngControl
/ ngFormControl` is enough...
Both allow to detect changes:
- Event ngModelChange
- Subscribe on ctrl.valueChanges
You could configure two-way binding for ngModel
for your form elements:
<form (ngSubmit)="editUser(f.value)" #f="ngForm">
<input ngControl="nameInp" [(ngModel)]="selectedUser.name" type="text">
<input ngControl="ageInp" [(ngModel)]="selectedUser.age" type="text">
<input ngControl="cityInp" [(ngModel)]="selectedUser.city" type="text">
<button type="submit">Save</button>
</form>
本文标签: javascriptAngular 2set value of text inputs in formStack Overflow
版权声明:本文标题:javascript - Angular 2, set value of text inputs in form - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739296502a2156934.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论