Can I use [ngModel] with async function?

My code is the following:

<ion-input [ngModel]="getValorationFilm(film.id')"></ion-input>

and getValorationFilm( ) is an async function and return a number.

It doesn’t work for me due to its a infinite loop.
How can I use ngModel with async funcition?
Thank you so much

I haven’t tested this, but does this work?
<ion-input [ngModel]="(getValorationFilm(film.id) | async)"></ion-input>

Thank you @MattE , unfortunately it doesn’t work for me, the infinite loop still alive …

Maybe something is wrong with your code
Can you show the getValorationFilm function?

i think [(ngModel)] is the correct one instead of [ngModel]

Ah yeah, that could be the issue.
I overlooked that one

@rlwt If i write [(ngModel)] the error is this:

<ion-input [ERROR ->][(ngModel)]="(getValorationFilm(film.id) | async)"></ion-input>
       "): ng:///AppModule/main.html@29:63
Parser Error: Unexpected token ), expected identifier or keyword at column 66 in [(getValorationFilm(film.id) | async)=$event]

@MattE my getValorationFilm function is the following:

public getValorationFilm(filmId: string){

this.filmSrv.getFilmById(filmId).then(
     film =>   return film.id
);
}

What do you mean that it’s an infinite loop?

And are you also sure that you want to be using a function here, particularly an async one? It will likely wind up not being terribly performant with how Angular’s change detection works.

Yes, propably you are right, it could a bad idea… but i dont know how get a value of each film inside of *ngFor

Hi, why not try a variable, and update it whenever the film.id value changes instead of setting your input to a function?
You can set your input’s model to a variable, and whenever the film.id value changes you can call your function to get the new value.

Hi, because i have a ion-list win a *ngFor and i want to display the value of each film.

Hello,

what cguzes mneans is, that you can do that also with a variable. I think you create a arrray, exsample films, to feed your ngFor.

Maybe yout array contains object like {film: ‘starwars1’} and you can extend it by {film: ‘starwars1’, id:filmId} and push this to your arry films

ngfor let fi in films and
[ngModel]=“fi.id

Oh man its late, I hope its understandable.

Best regards, anna-liebt

Its a good idea, you are right. Thank you guys :slight_smile: