简体   繁体   English

表格未预填充 Angular 8

[英]Form not pre-populated Angular 8

I have an API service which returns an array of objects.我有一个返回对象数组的 API 服务。 Each object has two fields name and usage.每个 object 都有两个字段名称和用法。 I am creating a form dynamically where the number of form fields equals the number of objects inside array.我正在动态创建一个表单,其中表单字段的数量等于数组内的对象数量。 Name of the form field is the name and it should be prepopulated with usage value.表单字段的名称是name ,它应该预先填充usage值。 I am able to create the form and name the form fields but the usage value is not displaying let along correct value in each form field.我能够创建表单并命名表单字段,但usage值未在每个表单字段中显示正确的值。 My TS file code:我的 TS 文件代码:

ngOnInit() {
    this.apiCall.getArray()
    .subscribe(
      (res: any) => {
        this.receivedData = res;
        for (let i = 0; i < this.receivedData.length; i ++){
          this.currentName  = this.receivedData[i].name;
          this.currentUsage = this.receivedData[i].usage;
          this.name.push(this.currentName);
          this.usage.push(this.currentUsage);
        }

name and usage are arrays where values are stored.名称和用法是存储值的 arrays。 In my HTML:在我的 HTML 中:

<form>
    <div class="form-row">
      <div class="form-group *ngFor="let plans of name">
        <label>{{plans}}</label>
        <input type="number" min="0" class="form-control" id="usage" name="usage" value="usage" [(ngModel)]="usage">
      </div>
      </div>
      </form>

Now my form fields show correct field names but the fields are empty.现在我的表单字段显示正确的字段名称,但字段为空。 They should be pre-filled with respective usage value and the user should be able to change the values as per they wish.它们应该预先填充各自的使用值,并且用户应该能够根据需要更改这些值。

I think there are some problems with your code.我认为您的代码存在一些问题。

First, if you want to assign a value to an attribute within an Angular *ngFor loop, you need to do something like item.foo .首先,如果您想为 Angular *ngFor循环中的属性分配值,您需要执行类似item.foo的操作。 Next to that, square brackets [] are needed to actually pass the value into the attribute and loop through the array of data, because the data is dynamic.接下来,需要方括号[]才能将值实际传递给属性并循环遍历数据数组,因为数据是动态的。

Last problem is that you can't assign numbers (I assumed item.usage would be a number ) to strings like id and name .最后一个问题是你不能将数字(我假设item.usage是一个number )分配给像idname这样的字符串。 It's also because you have added the type="number" to the input field.这也是因为您已将type="number"添加到input字段中。

This will result in the following setup in HTML.这将导致 HTML 中的以下设置。 I am looping through the data with let item of items and picking the keys where I need them.我正在使用let item of items遍历数据并在需要它们的地方选择键。

<form>
    <div class="form-row">
      <div class="form-group" *ngFor="let item of items">
        <label>{{item.name}}</label>
        <input type="number" min="0" [id]="item.name" [name]="item.name" class="form-control" [(ngModel)]="item.usage">
      </div>
  </div>
</form>

With some mock data it turns out to work.有了一些模拟数据,它就可以工作了。

  received = [
  {
    name: 'Field 1',
    usage: 123456,
  },
  {
    name: 'Field 2',
    usage: 4655454,
  },
  {
    name: 'Field 3',
    usage: 54445,
  }];

See this StackBlitz example for a working example, fixing all of the problems I mentioned earlier.请参阅这个 StackBlitz 示例以获得一个工作示例,它解决了我之前提到的所有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM