My *ngFor
doesnt update on Value Change. My relevant code:
chat.component.html
<div *ngFor="let contact of contactList; let i = index;">
{{contact.name}}
</div>
chat.component.ts
export class ChatComponent implements OnInit {
public selectedTeamId: string;
constructor(
private apiClientService :ApiClientService,
private teamService: SetTeamService
){
this.teamService.teamIdChange.subscribe(value => {
this.selectedTeamId = value;
this.apiClientService.getAPIObject(param, "chat-room-list").then((data) => {
this.contactList = data.response_payload.contacs;
});
});
}
}
setTeam.service.ts
export class SetTeamService {
public teamId:string = '';
teamIdChange: Subject<string> = new Subject<string>();
constructor( ) {
this.teamIdChange.subscribe((value) => {
this.teamId = value;
sessionStorage.setItem('teamId',value);
});
}
setTeamId(teamId){
this.teamIdChange.next(teamId);
}
}
The following steps should happen:
- The Set Team ID function get called with a new value
- The Subscribe in the constructer should get notified
- The component starts an GET Api Call with new param
- The *ngFor in the Template should iterate over the new Value in
contactList
Step 1, 2 and 3 are working. I get the value from my API and the contactList
updates.
Sadly Step 4 doesnt work. the Template doesnt react or rerender on the value change. Usually im doing this stuff on the NgOnInit and it works this way. But this time i need a subscribe on a service Value.
EDIT:
Value from the API Response:
{
"status": {
"statusCode": 0,
"statusMessage": "OK"
},
"response_payload": {
"contacs": [
{
"id": "0c9d8efc-fc8a-42fc-80d8-64532679df48",
"crm_account": "d9eec698-80d2-4976-8b79-9a900737848a",
"name": "XXX",
"email": "XXX",
"deleted": "0",
"update_date": "2019-07-25 05:05:48",
"create_date": "2019-07-25 05:05:48",
"hasChat": false
},
{
"id": "885c2c11-cd5b-461c-b6dd-efdf4e21966d",
"crm_account": "6615b728-c484-4f94-bc74-b214fab2a9e3",
"name": "YYY",
"email": "YYY",
"deleted": "0",
"update_date": "2019-08-02 15:02:15",
"create_date": "2019-08-02 15:02:15",
"hasChat": "1234"
}
]
}
}
navbar.component.html
<a *ngFor="let mandant of mandanten;let i=index;" class="dropdown-item" (click)="selectedTeam = mandant.name; setTeamId(mandant);">
navbar.component.ts
setTeamId(team) {
let teamId = team['team_id'];
this.teamService.setTeamId(teamId);
}