0

hello every one i have this api

[
    [{
        "id": 1,
        "qte": 12,
        "date_creation": "2020-08-17T00:00:00+02:00",
        "date_update": "2020-08-17T00:00:00+02:00",
        "depot": {
            "id": 1,
            "nom_depot": "bb"
        },
        "produit": {
            "id": 1,
            "matricule": "ff",
            "prix": 12,
            "libelle": "dd"
        }
    }],
    []
]

but when i want to display just the Qte ,the libelle of produit and nom_depot of depot like this

<tr *ngFor="let
data of
depot ;let
i= index"
(click)=addToAnotherTable(i)>
<td>
<span>{{data.depot.nom_depot}}</span>
</td>
<td>
<span>{{data.produit.libelle}}</span>
</td>
<td>
<span>{{data.qte}}</span>
</td>
</tr>

it doesn t work i don t know why pls help

3
  • check your data... it is array of arrays Commented Aug 25, 2020 at 15:48
  • If you're sure the parent array will always have a single element, you could do <tr *ngFor="let data of depot[0]; let i=index". Then if possible, it must be fixed in the backend to remove the parent array. Commented Aug 25, 2020 at 15:50
  • no i have a lot of elements Commented Aug 25, 2020 at 15:51

1 Answer 1

2

You just need to use index, because as Michalis said, you have an array of arrays.

datas = [
    [{
        "id": 1,
        "qte": 12,
        "date_creation": "2020-08-17T00:00:00+02:00",
        "date_update": "2020-08-17T00:00:00+02:00",
        "depot": {
            "id": 1,
            "nom_depot": "bb"
        },
        "produit": {
            "id": 1,
            "matricule": "ff",
            "prix": 12,
            "libelle": "dd"
        }
    }],
    []
];
<table>
    <tr *ngFor="let data of datas;let i= index">
        <td>
            <span>{{data[i]?.depot.nom_depot}}</span>
        </td>
        <td>
            <span>{{data[i]?.produit.libelle}}</span>
        </td>
        <td>
            <span>{{data[i]?.qte}}</span>
        </td>
    </tr>
</table>
Sign up to request clarification or add additional context in comments.

2 Comments

it displays the data but it gives me an infinit loop with this error TypeError: Cannot read property 'depot' of undefined
I edited my post adding ? just after data[i] please check.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.