简体   繁体   English

如何在 Angular forms 中设置字段值

[英]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”字段,而我的视图是空的。

在此处输入图像描述 Thanks for reading!谢谢阅读!

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"
/>

I think your problem might be in using ngModel and Value.我认为您的问题可能在于使用 ngModel 和 Value。

Refer to this response in another question for more clarification.请参阅另一个问题中的此回复以获取更多说明。

Short answer简短的回答

Remove the value property from the input tag.从输入标签中移除value属性。

Right answer Use angular forms read here .正确答案使用 angular forms阅读此处 After setting the from Group for your form set value for the right controller为您的表单设置 from Group 后,为右侧 controller 设置值

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.

相关问题 如何仅检测Angular 2表单中更改的字段? - How to detect only the changed fields in Angular 2 forms? Angular Forms:设置所有字段不变,不脏且有效 - Angular Forms: Set all fields untouched, not dirty and valid 如何在谷歌表单脚本编辑器中设置 var 主题以返回其所连接的表单/工作表中指定字段的值? - How can I set the var subject in google forms script editor to return the values of specified fields in the form/sheet its is connected to? 如何将传递的值设置为模板的字段? - How to set passed values into template's fields? 如何在 Odoo 14 中为字段设置默认值 - How to set default values to fields in Odoo 14 如何使用反应式 forms 设置预选下拉菜单? Angular - How to set pre-select dropdown with reactive forms? Angular 如何以Angular 7反应形式设置HTML选择元素的选定值? - How to set selected value of HTML Select Element in Angular 7 reactive forms? Angular 6 反应式 Forms:如何将焦点设置在第一个无效输入上 - Angular 6 Reactive Forms : How to set focus on first invalid input 如何使用 Angular 8 在组件表单内设置 tabindex 顺序 - how to set tabindex order inside the component forms using angular 8 Angular 2动态表单如何根据另一个字段的值显示/隐藏字段 - Angular 2 Dynamic Forms How to show/hide a field based on the values of another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM