如何在打字稿中显示对象值

发布于 2025-02-08 14:33:10 字数 1152 浏览 1 评论 0原文

如何以角度显示对象值。在浏览器开发人员工具中,我得到的对象及其值可以很好。但是无法以形式填充值。

在 *.ts文件中,我得到了:

console.log(this.product),              // {"productId": "3", "productName": "G500 cpu", "productDescription": "gaming computer", "productCategory": "computers", "units": 5 }
this.updateForm.patchValue({
        // productId: '23128',                // display 23128
        //   productName: 'asdas',            // display asdas
        productId: this.product.productId,    // doesnt display anything
        productName: this.product.productName // doesnt display anything
        // productId: this.product,    // display [object Object]
        // productName: this.product // display [object Object]
      })

在 *.html中,我得到了:

    <form [formGroup]="updateForm">
      <div class="form-group">
        <input
        type="text"
        class="form-control"
        formControlName="productId">
      </div>
      <div class="form-group">
        <input
          type="text"
          class="form-control"
          formControlName="productName"
        />
      </div>

How to display object value in forms in angular. In browser developer tools I am getting the object and its values fine. But can't populate the values in the form.

In *.ts file i got:

console.log(this.product),              // {"productId": "3", "productName": "G500 cpu", "productDescription": "gaming computer", "productCategory": "computers", "units": 5 }
this.updateForm.patchValue({
        // productId: '23128',                // display 23128
        //   productName: 'asdas',            // display asdas
        productId: this.product.productId,    // doesnt display anything
        productName: this.product.productName // doesnt display anything
        // productId: this.product,    // display [object Object]
        // productName: this.product // display [object Object]
      })

in *.html I got:

    <form [formGroup]="updateForm">
      <div class="form-group">
        <input
        type="text"
        class="form-control"
        formControlName="productId">
      </div>
      <div class="form-group">
        <input
          type="text"
          class="form-control"
          formControlName="productName"
        />
      </div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉梦枕江山 2025-02-15 14:33:10

而不是分配每个控制值补丁程序对象的整体值,而是
只需确保将“控制名称”和“对象变量名称”匹配。

stackblitz示例

this.updateForm = this.formBuilder.group({
      productId: new FormControl(''),
      productName: new FormControl(''),
      productDescription: new FormControl(''),
      productCategory: new FormControl(''),
      units: new FormControl('')
    });
    
    var product = {
      productId: "3",
      productName: "G500 cpu",
      productDescription: "gaming computer",
      productCategory: "computers",
      units: 5 
    };
    this.updateForm.patchValue(product);

Instead of assigning each control value patch whole value of product object,
just make sure form control name and object variable name should be matched.

stackblitz example

this.updateForm = this.formBuilder.group({
      productId: new FormControl(''),
      productName: new FormControl(''),
      productDescription: new FormControl(''),
      productCategory: new FormControl(''),
      units: new FormControl('')
    });
    
    var product = {
      productId: "3",
      productName: "G500 cpu",
      productDescription: "gaming computer",
      productCategory: "computers",
      units: 5 
    };
    this.updateForm.patchValue(product);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文