Ho un problema con ngOnChange
. Ho seguenti componenti:Come gestire gli input asincroni nei componenti?
@Component({
selector:'user-table',
template: `...`,
})
export class UserTable implements OnChanges{
@Input() users: User[];
years:string[];
constructor(private _usersCollection:UsersCollection){
}
ngOnChanges(){
if (this.users.length)
{this.years =this._usersCollection.createYearsArray(this.users)}
}
}
Tuttavia, se unica condizione viene controllato una volta - quando this.users
non è ancora recuperato dal server, e quindi la sua lunghezza è pari a 0. Come faccio a trovare la soluzione per affrontare questo tipo di asincrona ingressi?
La matrice viene aggiornato, come quando ho impostato i seguenti registri:
console.log('ON FIRST INIT' , this.programs);
this.years = this._usersCollection.createYearsArray();
console.log(this.years);
setInterval(()=>{
console.log('IN INTERVVAL' , this.programs);
},1000);
L'uscita della console è:
ON FIRST INIT []
UsersTable.component.ts:21 []
UsersTable.component.ts:23 IN INTERVVAL [Object, Object, Object, Object]
Difficile vedere da questo codice cosa sta succedendo. Dove stai andando a prendere i dati. Come sono passati al componente 'UserTable'? –
Sono in macchina - aggiornerò q in 30 ':) – uksz