Angular 5,在html中显示json

By simon at 2018-02-07 • 0人收藏 • 33人看过

在我的Angular 5项目中,我想在HTML代码参数中显示 “defaultprice”:100,但是我的代码显示了所有的defaultprice。component.ts

   this.ws.getAllProduct().subscribe(
      products => {
          this.products = products.map((product) => {
          return new Product(product);
        });
      }
    );
component.html
  <div class="row">
    <div class="input-field col s12">
      <label for="unit_price" *ngFor="let item of products">{{item.default_price}}</label>
      <input formControlName="unit_price" id="unit_price" type="number" class="validate" placeholder="unit_price">
    </div>
  </div>
JSON:

{"StatusCode":0,"StatusMessage":"OK","StatusDescription":
[
{"product_id":"1",
"product_name":"Product1",
"description":"Product1",
"default_price":50,
"lastmoduserid":"31",
"lastmoddtm":"2018-02-06T13:26:17.000Z",
"active":1},
{"product_id":"2",
"product_name":"Product2",
"description":"Product2",
"default_price":60,
"lastmoduserid":"31",
"lastmoddtm":"2018-02-06T13:35:17.000Z",
"active":1},
{"product_id":"3",
"product_name":"Product3",
"description":"Product3",
"default_price":80,
"lastmoduserid":"31",
"lastmoddtm":"2018-02-06T13:35:22.000Z",
"active":1},
{"product_id":"4",
"product_name":"Product4",
"description":"Product4",
"default_price":100,
"lastmoduserid":"31",
"lastmoddtm":"2018-02-06T13:25:54.000Z",
"active":1},
{"product_id":"5",
"product_name":"Product5",
"description":"Product5",
"default_price":120,
"lastmoduserid":"31",
"lastmoddtm":"2018-02-06T13:35:27.000Z",
"active":1}
]}
我只想在html代码中显示“productid”:“4”,“PRODUCTNAME”: “产品4”。 你能告诉我一些想法吗?日Thnx

1 个回复 | 最后更新于 2018-02-07
2018-02-07   #1

你不需要ngFor如果你需要显示特定的对象,你可以使用 array.find()查找数组中的特定元素

登录后方可回帖

Loading...