Angular template not picking up the changes from the subscription
See original GitHub issueHi,
I am using apollo-angular
+ subscriptions-transport-ws
in my project. I have the client setup to listen to the subscription for user’s password updated or new user is created event. This is how it is setup
//Apollo Subscription
this.apollo.subscribe({
query: this.passwordUpdatedSubscription
}).subscribe(({passwordUpdated}:any) => {
return this.userDataQuery.updateQuery((previousResult) => {
let userIndex = previousResult.getUsers.findIndex(user => user.name === passwordUpdated.name);
let updatedUsers;
if (userIndex === -1) {
updatedUsers = [].concat(previousResult.getUsers).concat([passwordUpdated]);
} else {
updatedUsers = [].concat(previousResult.getUsers.slice(0, userIndex)).concat([passwordUpdated]);
if (userIndex !== previousResult.getUsers.length - 1) {
updatedUsers = updatedUsers.concat(previousResult.getUsers.slice(userIndex + 1));
}
}
return Object.assign({}, previousResult, {
getUsers: updatedUsers,
});
});
});
It seems to be working fine and does its job to notify the userDataQuery
about the update. In the userDataQuery
, it handles the updates this way
this.userDataQuery = this.apollo.watchQuery({
query: this.getUsersQuery
}).subscribe(({data}:any) => {
this.userData = data.getUsers;
// I have to call detectChanges to tell Angular to render the template.
// is there anyway to avoid doing this?
//this.ref.detectChanges();
});
The userData did get updated successfully, However, the changes is not reflected on the template. I will have to call this.ref.detectChanges();
to update the template.
The template is very simple
<span ng-if="userData">{{userData && userData[0] && userData[0].password}}</span>
Is there anything I missed in my code. I thought the template would pick up the change automatically ?
Issue Analytics
- State:
- Created 7 years ago
- Reactions:2
- Comments:16 (5 by maintainers)
Top Results From Across the Web
Angular: Subscribe not detecting changes therefore my view ...
Don't worry about it, it's a common mistake at first. When you subscribe to an Observable, Angular not put a "big brother" to...
Read more >Angular template not picking up the changes from the ... - GitHub
Hi,. I am using apollo-angular + subscriptions-transport-ws in my project. I have the client setup to listen to the subscription for user's ...
Read more >Component testing scenarios - Angular
The Angular testing environment does not know that the test changed the component's title . The ComponentFixtureAutoDetect service responds to asynchronous ...
Read more >How to forget about the management of subscriptions and ...
I find that most of the articles regarding the Angular framework describe issues with subscription management and change detection ...
Read more >Angular Change Detection - How Does It Really Work?
This is because lastname is not used in the component template ! Also, the top-level id property of Todo is not compared by...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
not working for me i checked it with v1.0. i wanted to show number of updates per millisecond with updates property. it wouldn’t work. i have OnPush set on the component
Works in v1.0