Angular 6 错误:找不到“字符串"类型的不同支持对象 [英] Angular 6 Error: Cannot find a differ supporting object of type 'string'

查看:23
本文介绍了Angular 6 错误:找不到“字符串"类型的不同支持对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如您在主题中看到的,我收到此错误是因为数据库中的一列是一串 JSON,我想我可以通过在 ngFor 循环中使用 ngFor 循环来解决这个问题.

以下是html:

 <tr *ngFor="let item of mf.data"><td>{{ item.id }}</td><td>{{ item.user_id }}</td><td class="text-right">{{ item.orders_id }}</td><td><ul *ngFor="let x of item.product"><li>{{ x.name }}</li><li>{{ x.price }}</li><li>{{ x.category }}</li><li>{{ x.ts }}</li><li>{{ x.enabled }}</li><li>{{ x.counter }}</li></td></tr></tbody>

下面是product"列的一行:

<预><代码> [{"id": "13","name": "测试 5","价格": "3.42",类别":巧克力","ts": "2019-01-08 10:41:15","product_image_id": "50",启用":1","product_image": "40-64-grand-canyon.png",计数器":2"},{"id": "18","name": "测试 4 个帖子配音","价格": "6.72",类别":巧克力","ts": "2019-01-08 08:55:49","product_image_id": "36",启用":1","product_image": "first-ent-rent-ridgegate.png",计数器":3"},{"id": "9","name": "测试 3 upd","价格": "12.23",类别":巧克力","ts": "2019-01-08 08:54:49","product_image_id": "29",启用":1","product_image": "80-44-grand-canyon.png",计数器":2"}]

顺便说一句,我尝试了以下操作,没有错误,但也没有任何显示:

 <tr *ngFor="let item of mf.data"><td>{{ item.id }}</td><td>{{ item.user_id }}</td><td class="text-right">{{ item.orders_id }}</td><td><ul *ngFor="let x of mf.data.product"><li>{{ x.name }}</li><li>{{ x.price }}</li><li>{{ x.category }}</li><li>{{ x.ts }}</li><li>{{ x.enabled }}</li><li>{{ x.counter }}</li></td></tr></tbody>

同时尝试以下操作,但未定义产品的错误:

<tr *ngFor="let item of mf.data"><td>{{ item.id }}</td><td>{{ item.user_id }}</td><td class="text-right">{{ item.orders_id }}</td><td><ul *ngFor="let x of mf.data[i].product; let i = index"><li>{{ x.name }}</li><li>{{ x.price }}</li><li>{{ x.category }}</li><li>{{ x.ts }}</li><li>{{ x.enabled }}</li><li>{{ x.counter }}</li></td></tr></tbody>

提前致谢

解决方案

这是因为 product 对象没有转换为 JSON 以及其他给定的数据,因为它是嵌套对象.

在您的模板中,调用将product字符串转换为JSON对象

的方法

在您的组件中创建一个方法,如下所示

convertToJSON(product: any) {返回 JSON.parse(product);}

As you can see by the subject, I am receiving this error because one of the columns in the database is a string of JSON and I thought I could get around this by having an ngFor loop within an ngFor loop.

The following is the html:

 <tbody>
  <tr *ngFor="let item of mf.data">
    <td>{{ item.id }}</td>
    <td>{{ item.user_id }}</td>
    <td class="text-right">{{ item.orders_id }}</td>
    <td>
      <ul *ngFor="let x of item.product">
        <li>{{ x.name }}</li>
        <li>{{ x.price }}</li>
        <li>{{ x.category }}</li>
        <li>{{ x.ts }}</li>
        <li>{{ x.enabled }}</li>
        <li>{{ x.counter }}</li>
      </ul>
    </td>
  </tr>
</tbody>

The following is what one row of the column "product" looks like:

 [
  {
    "id": "13",
    "name": "test 5",
    "price": "3.42",
    "category": "chocolates",
    "ts": "2019-01-08 10:41:15",
    "product_image_id": "50",
    "enabled": "1",
    "product_image": "40-64-grand-canyon.png",
    "counter": "2"
  },
  {
    "id": "18",
    "name": "test 4 post dubs",
    "price": "6.72",
    "category": "chocolates",
    "ts": "2019-01-08 08:55:49",
    "product_image_id": "36",
    "enabled": "1",
    "product_image": "first-ent-rent-ridgegate.png",
    "counter": "3"
  },
  {
    "id": "9",
    "name": "something test 3 upd",
    "price": "12.23",
    "category": "chocolates",
    "ts": "2019-01-08 08:54:49",
    "product_image_id": "29",
    "enabled": "1",
    "product_image": "80-44-grand-canyon.png",
    "counter": "2"
  }
]

BTW, I have tried the following, with no errors, but nothing displays as well:

 <tbody>
  <tr *ngFor="let item of mf.data">
    <td>{{ item.id }}</td>
    <td>{{ item.user_id }}</td>
    <td class="text-right">{{ item.orders_id }}</td>
    <td>
      <ul *ngFor="let x of mf.data.product">
        <li>{{ x.name }}</li>
        <li>{{ x.price }}</li>
        <li>{{ x.category }}</li>
        <li>{{ x.ts }}</li>
        <li>{{ x.enabled }}</li>
        <li>{{ x.counter }}</li>
      </ul>
    </td>
  </tr>
</tbody>

Along with trying the following, but errors of product not being defined:

<tbody>
  <tr *ngFor="let item of mf.data">
    <td>{{ item.id }}</td>
    <td>{{ item.user_id }}</td>
    <td class="text-right">{{ item.orders_id }}</td>
    <td>
      <ul *ngFor="let x of mf.data[i].product; let i = index">
        <li>{{ x.name }}</li>
        <li>{{ x.price }}</li>
        <li>{{ x.category }}</li>
        <li>{{ x.ts }}</li>
        <li>{{ x.enabled }}</li>
        <li>{{ x.counter }}</li>
      </ul>
    </td>
  </tr>
</tbody>

Thanks in advance

解决方案

This is because of product object which is not converted into JSON along with other given data as it is nested object.

In your template, call the method to convert product string to JSON object

<ul *ngFor="let x of convertToJSON(item.product)">

Create a method in your component like below

convertToJSON(product: any) {
    return JSON.parse(product);
}

这篇关于Angular 6 错误:找不到“字符串"类型的不同支持对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆