【问题标题】:Angular Reactive Forms Input Error (input field not showing)Angular Reactive Forms 输入错误(输入字段未显示)
【发布时间】:2020-12-22 12:40:03
【问题描述】:

我创建了一个动态表单 (Angular 10),它通过调用后端 (Django/DjangoRestFramework) 动态获取 FormControlNames

FormControlNames 是我希望用户看到的文章的名称。用户必须在每个绑定到正确FormControlName 的输入字段中写入每篇文章显示的数量,然后提交整个内容。

问题:尽管我可以看到 FormControlNames 被正确创建(显示):

  {{myForm?.value|json}}

用户不能为新创建的 FormControlNames 设置任何值,因为输入字段不会出现。 我该怎么做才能显示输入字段框?我在控制台上没有收到任何错误。只是没有出现输入框。

Screenshot

后端文件/响应:

[
    {
        "id": 16,
        "nomeart": "Gambero Abbattuto",
        "completed": false,
        "desart": "code abbattute alla fonte",
        "codiceart": "zgamberiiiiii",
        "peso": 1,
        "taglia": 0,
        "um": "kg"
    },
    {
        "id": 15,
        "nomeart": "Branzino Fresco",
        "completed": false,
        "desart": "è un branzino",
        "codiceart": "ilbraaaa",
        "peso": 1,
        "taglia": 0,
        "um": "kg"
    },
    {...}

]

文件:component.ts

import { Component, OnInit } from '@angular/core';
import { ServerService } from 'src/app/server.service';
import { DataService } from "./data.service";
import { AuthService } from 'src/app/auth.service';
import { FormBuilder, FormGroup,
  Validators, FormArray,
  ReactiveFormsModule, FormControl } from '@angular/forms';
import { delay } from 'rxjs/operators';

  interface articoli {
    id: 'number',
    nomeart: 'string',
    completed: 'boolean',
    desart: 'string',
    codiceart: 'string',
    peso: 'number',
    taglia: 'number',
    um: 'string',
  }

  interface Pezzi {
    value: string;
  }


@Component({
  selector: 'app-ordine',
  templateUrl: './ordine.component.html',
  styleUrls: ['./ordine.component.scss']
})
export class OrdineComponent implements OnInit {

  myForm: FormGroup;

  articoli: any;

  articolo= {
    id: 'number',
    nomeart: 'string',
    completed: 'boolean',
    desart: 'string',
    codiceart: 'string',
    peso: 'number',
    taglia: 'number',
    um: 'string',
  }

  constructor(
    private server: ServerService,
    private authService: AuthService,
    public dialog: MatDialog,
    private fb: FormBuilder,
    private dataService: DataService
  ) {}

  ngOnInit() {

    this.server.request('GET', '/api/v1/articolo-list/')
    .subscribe((data: any) => {
      if (data) {
        this.articoli = data;
      }
    });

    this.myForm = this.fb.group({
      title: ["", []],
      articoli: this.fb.group({})
    });
    this.getThings();

  }

  getThings() { 
    this.dataService.getThings().subscribe(data => {
      this.articoli = data;
      this.dataService.arty.forEach(x => {
        (this.myForm.get("articoli") as FormGroup).addControl(
          x.nomeart,
          this.fb.control("0")
        );
      });
    });

  }



  nrpezzi: Pezzi[] = [
    { value: '0'},
    { value: '1'},
    { value: '2'},
    { value: '3'},
    { value: '4'},
    { value: '5'},    
    { value: '6'},
    { value: '7'},
    { value: '8'},
    { value: '9'},
    { value: '10'},
    { value: '11'},
    { value: '12'},
    { value: '13'},
    { value: '14'},
    { value: '15'},
    { value: '16'},
    { value: '17'},
    { value: '18'},
    { value: '19'},
    { value: '20'},
    { value: '21'},
    { value: '22'},
    { value: '23'},
    { value: '24'},
    { value: '25'},
    { value: '26'},
    { value: '27'},
    { value: '28'},
    { value: '29'},
    { value: '30'},
    { value: '31'},
    { value: '32'},
    { value: '33'},
    { value: '34'},
    { value: '35'},
    { value: '36'},
    { value: '37'},
    { value: '38'},
    { value: '39'},
    { value: '40'},
    { value: '41'},
    { value: '42'},
    
  ];

}

文件:dataservice.ts

import { Injectable, OnInit } from "@angular/core";
import { of } from "rxjs";
import { delay } from "rxjs/operators";
import { ServerService } from 'src/app/server.service';
import { AuthService } from 'src/app/auth.service';
import { OrdineComponent } from './ordine.component';

interface articoli {
  id: 'number',
  nomeart: 'string',
  completed: 'boolean',
  desart: 'string',
  codiceart: 'string',
  peso: 'number',
  taglia: 'number',
  um: 'string',
}

@Injectable({
  providedIn: "root"
})

export class DataService {

  constructor(
    private server: ServerService,
    private authService: AuthService,
  ) {}

  
  arty:articoli[] =[];


  articolo= {
    id: 'number',
    nomeart: 'string',
    completed: 'boolean',
    desart: 'string',
    codiceart: 'string',
    peso: 'number',
    taglia: 'number',
    um: 'string',
  }

  
  getThings() {

    this.server.request('GET', '/api/v1/articolo-list/')
    .subscribe((data: any) => {
      if (data) {
        this.arty = data;
        console.log(data);
      }
    });

    return of(
   [this.articolo.nomeart]
  ).pipe(delay(200));
  }
}

文件:component.html

<form [formGroup]="myForm">
    ...
    <div formGroupName="articoli">
        <mat-card *ngFor="let articolo of arty">
            <input [formControlName]=
            "articolo.nomeart"> 
        </mat-card>
    </div>
  </form>
  {{myForm?.value|json}}
  <hr/>

有人知道我做错了什么吗?

【问题讨论】:

    标签: angular forms form-control formgroups


    【解决方案1】:

    在上面的行中,只是检查您是否在显示 html 的各个组件 ts 层中分配了 arty 的值,请检查 arty 列表值是否与您检查 myform 时一样

    【讨论】:

    • 抱歉,我不明白您要解释什么。为简单起见,考虑我放了“div”而不是“mat-card”:“
      ”;拜托,您能否再次向我解释您的建议“您是否在显示 html 的各个组件 ts 层中分配了 arty 的值”?我应该如何检查艺术列表值是否与您检查 myform 时一样?
    • 这里你将输入元素放在 ng for 循环中,所以如果 arty 的值为空,输入元素将不会渲染,所以请检查 arty 列表示例的值,例如 { html 层中的 {arty|json}}
    • 是的,你没看错,arty 的值为 null!我以为我正在用 api 调用填充它,但我错了。那么,现在,你对我如何解决这个问题有什么建议吗?感谢您的宝贵时间!
    • 我正在做无限测试,如果我使用“让 articolo of arty”,FormControlnames 会像你在我的问题中看到的那样创建。如果我尝试使用“let articolo of articoli”,输入框会出现但无法使用,因为这次“找不到路径控制:articoli -> Gambero Abbattuto”(gambero abbattuto 是各种 articolo.nomeart 的值)。所以,如果我能得到 FormControlNames,我就不能得到输入框,反之亦然。有什么想法吗?
    • @utenteserio 感谢您提供的信息,所以当值存在时,您可以看到输入元素我正确吗?抱歉,我无法理解从 api 调用获取数据还是获取 formControlName 所面临的问题。假设如果您询问如何获取暗示这一点的数据 - 您应该通过从数据服务层获取数据来在组件层中分配 arty 属性值,您需要从数据服务中获取 arty 数据并应该分配值。
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签