Reputation: 24472
I have the following code:
<view-user *ngIf="selectedUser" [userID]="selectedUser.id"></view-user>
and an API call: loadUser(userID)
in my view-user
component, in the
ngOnInit()
whenever the selectedUser is changed, the ngOnInit is not being called and the layout is not changing. it means I see the same user although the selectedUser is different.
I believe the ngOnInit()
is called only in the first time, and after that the variable changes but the ngOnInit() is not being callede anymore.
How I can run my loadUser()
method whenever the userID
is changed?
Upvotes: 0
Views: 26
Reputation: 691893
You can use a setter to change your userID:
private _userID: number;
get userID() {
return this._userID;
}
@Input()
set userID(userID: number) {
this._userID = userID;
loadUser(userID);
}
Or you can use the ngOnChanges
method.
Upvotes: 1