angularjs - Angular2 Object Instantiation for Asynchronous Data -
i working on angular2 project gets object asynchronously api using new observable framework.
this._accountsservice.getaccountdetails(this.id) .subscribe( accountdetails => this.accountdetails = accountdetails, err => this.errormessage = <any>err, () => console.log(this.accountdetails) );
when tried access property of object using interpolation {{accountdetails.username}} ,we got property undefined error, because object had not yet been received api. fixed instantiating object in class
accountdetails: accountdetails = new accountdetails();
this works, noticed in angular 1, fake object created, if object undefined, there never an undefined error -- show empty value in interpolated code. change in angular2? also, noticed when used *ngfor in our code iterate through properties of object, display empty value (rather trigger object undefined error) if object had not yet been received api. occurs internally in *ngfor directive?
with first piece of code need following things,
1) because should {{accountdetails.username}} (not {{accountdetails.username}} ). note : please double check u or u can't tell without knowing object's properties.
2) use ?.
{{accountdetails?.username}}
i hope help.
Comments
Post a Comment