How to pass values created by ngFor as function parameter Ionic 3 + Angular + Firebase?

0

It's my first time asking in the forum so if I'm doing something wrong please correct me.

I have the following excerpt from my HTML page:

Request:

 <ion-list *ngFor = "let p of produtos | async">
      <ion-input type = "text" [disabled] = "true" value = {{p.descricao}}></ion-input>  
      <ion-input type = "text" [disabled] = "true" value = {{p.preco}}></ion-input>  
    </ion-list>

And I would like to click the save button to pass as parameter the values generated by ngFor

<div padding>
  <button ion-button type='submit' (click) = 'salvar(p.descricao, p.preco)'>
    <ion-icon name="cash"></ion-icon>Comprar</button>

But whenever I click on it, it generates the following error:

ERROR TypeError: Cannot read property 'descricao' of undefined
at Object.eval [as handleEvent] (FinalizarPedidoPage.html:93)
at handleEvent (core.js:13589)
at callWithDebugContext (core.js:15098)
at Object.debugHandleEvent [as handleEvent] (core.js:14685)
at dispatchEvent (core.js:10004)
at core.js:10629
at HTMLButtonElement.<anonymous> (platform-browser.js:2628)
at t.invokeTask (polyfills.js:3)
at Object.onInvokeTask (core.js:4751)
at t.invokeTask (polyfills.js:3)

I have already noticed that the error is in passing the data as array, because if I only pass as a single value the function is executed normally.

How can I resolve this problem?

    
asked by anonymous 31.05.2018 / 08:21

2 answers

0

Try:

<ion-input type = "text" [disabled] = "true" [(ngModel)]="p.descricao"></ion-input>

Do not forget to import FormsModule into module

    
31.05.2018 / 10:50
0

For this error to happen, you should declare " products " as an Array.

produtos = [];

The description index will continue the loop without any errors.     

02.06.2018 / 16:24