Hello, I am currently diving into Angular 2 and working on a project that involves basic CRUD functionality. I am encountering an issue with my User
component where I am trying to access its property in the ngAfterViewInit
hook, but it keeps showing as undefined
.
Take a look at the code snippet for the User
component below:
export class UserComponent implements OnInit, AfterViewInit
{
private users: User[];
private userRoles;
ngOnInit(){
//get users from service
this.getUserDetails();
}
ngAfterViewInit() {
console.log(this);
console.log(this.users);
}
getUserDetails() {
this._userService.getUsers().subscribe(users => this.users = users);
}
}
Below are screenshots from the console. You can see that the object has the property users
, but it still displays as undefined
in the console.
https://i.sstatic.net/I75no.png
https://i.sstatic.net/ydxox.png
If you have any suggestions or if I missed something, please feel free to share. Thank you in advance.