[英]How to set fields values in Angular forms
I'm trying to set view field values when "user_details" object is populated in the.ts on the ngOnInit() execution.当“user_details”object 在 ngOnInit() 执行的 .ts 中填充时,我正在尝试设置视图字段值。
This is the.ts where I'm filling the user_details object.这是我正在填写 user_details object 的 .ts。
export class ProfileComponent implements OnInit {
user_details = {
name: '',
first_surname: '',
second_surname: '',
age: '',
birdth_date: '',
location: '',
phone: '',
image: ''
};
constructor(private authService: AuthService, private router: Router) { }
ngOnInit(): void {
this.authService.getProfile()
.subscribe(
res => {
this.user_details = res;
console.log(this.user_details)
},
err => console.log(err)
);
}
As you see, the object is getting correct data.如您所见,object 正在获取正确的数据。
This is the form where I need to fill the fields.这是我需要填写字段的表格。
<div class="container p-4">
<form (submit)="updateProfile()">
<div class="row d-flex justify-content-center text-white">
<div class="col-md-6">
<div class="form-group">
<label for="name">Name</label>
<input type="text" [(ngModel)]="user_details.name" value={{user_details.name}} name="name" class="form-control" id="name" placeholder="Name">
</div>
<div class="form-group">
<label for="first_surname">First surname</label>
<input type="text" [(ngModel)]="user_details.first_surname" name="first_surname" value={{user_details.first_surname}} class="form-control" id="first_surname" placeholder="First surname">
</div>
...
How can I set the values of the view fields with this object data?如何使用此 object 数据设置视图字段的值? I need to be displayed "user_details" fields in the view, and my view is empty.
我需要在视图中显示“user_details”字段,而我的视图是空的。
The Best way is to use Reactive Forms , as it provides lot of features, which you may need in forms.最好的方法是使用反应式 Forms ,因为它提供了很多功能,您可能在 forms 中需要这些功能。 I have tried to recreate your code in code sandbox , please have a look at it, as I was able to solve it,
我试图在代码沙箱中重新创建您的代码,请看一下,因为我能够解决它,
<div class="form-group">
<label for="first_surname">Name</label>
<input
type="text"
[(ngModel)]="user_details.name.first"
name="first_surname"
value="user_details.name.first"
class="form-control"
id="name"
placeholder="Name"
/>
the way I solved that problem was by using 1st reactiveFroms.我解决这个问题的方法是使用 1st reactiveFroms。 just like other answers mentioned and here is how I did it.
就像提到的其他答案一样,这就是我的做法。
there are two ways your user will have their data on the component but in most cases, it will be called from the db which means a promise or an observable.您的用户有两种方法可以在组件上获取他们的数据,但在大多数情况下,它将从 db 调用,这意味着 promise 或 observable。
in that case.在这种情况下。 the psuedo code will look something like this.
伪代码看起来像这样。
ngOnInit() { this.form = new FormGroup({ firstName: new FormControl(null,), lastName: new FormControl(null,), mobile: new FormControl(null), }); this.YourService.getProfileInformation().subscribe((data: UserProfile) => { // you might want to check if your data is right and fits the form fields this.form.setValue({ firstName: data.firstName, lastName: data.lastName, mobile: data.mobile }) }) }
I believe this is the cleanest way to do that.我相信这是最干净的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.