question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Angular template not picking up the changes from the subscription

See original GitHub issue

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 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:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:16 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
BhaskaranRcommented, Dec 4, 2017

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

   this.apollo.subscribe({
            query: getExecutionsSubscription,
            variables: { filterId: '' },
        }).subscribe((subscriptionData: any) => {
            const tradeupdates: TradeUpdates[] = subscriptionData.getExecutions;
            this.updates = tradeupdates.length;
            }
        });
0reactions
kamilkisielacommented, Nov 10, 2017

Works in v1.0

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found