As the staff commented, starting this processing in the template is not the most indicated but focusing on your question directly you could do as follows:
<div *ngFor="let objeto of objetos">
<!-- Criar um array de uma única posição c/o resultado do pegarDados() -->
<ng-container *ngFor="let temp of [pegarDados(objeto)]">
<div [ngClass]="{'minha-classe': !!temp.isValido}">
{{ temp.nome }}
</div>
</div>
It’s not an elegant solution but it would work in the context you proposed. Remember that you can initialize this processing at some point in the life cycle of your component and use for example, the async pipe
to render the data in the template.
Turning to the question of the variable in the template, there is another interesting reference using directive: https://stackoverflow.com/questions/38582293/how-to-declare-a-variable-in-a-template-in-angular
Dude, blah through the string Interpolation {{ }} you can run Javascript in Html, but this is not indicated, especially when the function is asynchronous. Html is structure and Ts is code.
– LeAndrade
My focus is not on the asynchronous function itself, but on how I can save the data of a generic function into a variable in the block I will work on
– Arthur Siqueira
I don’t quite understand your doubt, Let object is no longer storing the data?
– LeAndrade
but how can I save the data returned by function
pegaDados()
in a variable?– Arthur Siqueira
It would be good to enter the code of the Ts of the stuck function().
– LeAndrade
have you tried to make a variable receive the function? Type
const getData = pegarDados()
, and then call at the angle with the interpolation{{ getData.nome }}
?– adventistaam
@Leandrade the function returns an observable of an object
interface Resposta{id: number, isValido: boolean, nome: string}
as follows:pegaDados(id:number){return this.httpService.get(this.API_ADDRESS + "/" + id);}
– Arthur Siqueira
@adventistaam does not apply, I am trying to save the data in the template
– Arthur Siqueira