Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Binding value didn't update after asynchronous change #16343

Closed
montylab opened this issue Apr 26, 2017 · 6 comments
Closed

Binding value didn't update after asynchronous change #16343

montylab opened this issue Apr 26, 2017 · 6 comments
Labels
area: core Issues related to the framework runtime

Comments

@montylab
Copy link

montylab commented Apr 26, 2017

I'm submitting a

[x] bug report => search github for a similar issue or PR before submitting

I saw similar issue here: #7381
But it doesn't helped me, since current angular version isn't using babel

Current behavior
After asynchronous function is called, variable changes to true, but *ngIf doesn't showing up the item
After 5-30 seconds it detect variable change and showing item

Expected behavior
After asynchronous function is called, variable changes to true => *ngIf should showing up the item instantly

Minimal reproduction of the problem with instructions

https://angular-binding-bug.firebaseapp.com/ - demo

  1. open console
  2. try to sign in using google
  3. see that username doesn't appear, but console output showed correct results

https://github.com/montylab/angular2_cd_bug

<li *ngIf="isAuth" class="user"> 
    {{username}}
...

LI doesn't showing up after isAuth changed to true

this.authService.onAuthStateChange.subscribe((isAuth) => {
	this.username = this.authService.getUsername();
	this.isAuth = isAuth;
	console.log('fires subject - ', 'isAuth: ' + isAuth, 'name: '+ this.username);
	console.log('context: ', this);
});

What is the motivation / use case for changing the behavior?

Please tell us about your environment:

Windows 10, PHP Storm, npm, ng cli serve

  • Angular version: 4.0.3
  • Browser: Chrome (sometimes it works, try incognito mode), probably all.
  • Language: Typescript 2.2.0
@vicb vicb added the area: core Issues related to the framework runtime label Apr 27, 2017
@vicb
Copy link
Contributor

vicb commented Apr 27, 2017

Observable subscriptions are not executed in the Angular zone, please try:

this.authService.onAuthStateChange.subscribe((isAuth) => ngZone.run((isAuth) => {
  // your original code
}));

@montylab
Copy link
Author

montylab commented Apr 27, 2017

Hi @vicb, thanks for reply

I know about solution with ngZine.run, but for me it looks like overkill.
I've also tried EventEmitter instead of Observable, it doesn't work too.

emitter = new EventEmitter<boolean>();

...

this.emitter.emit(!!user);
this.authService.emitter.subscribe((isAuth) => {
    this.username = this.authService.getUsername();
    this.isAuth = isAuth;
        console.log('fires emitter - ', 'isAuth: ' + isAuth, 'name: '+ this.username);
        console.log('context: ', this);
});

Also I want to notice, that I use the same idea with Rx/Subject in another place, it works as expected

onRoomsUpdate = new Subject<Room[]>();
...
this.rooms = this.roomsService.getRooms();    // get rooms synchronously

this.roomsService.onRoomsUpdate.subscribe((rooms: Room[])=> {   
    this.rooms = rooms;    // update rooms when they were changed
})

Can you please clarify why it happens?
I want to understand behavior of these two cases and what is the difference between them.

Thank you,
Eduard

@tytskyi
Copy link

tytskyi commented Apr 27, 2017

@montylab i guess it's because you emit new value inside of firebase.auth() callback - which knows nothing about the angular zone

@Klinton90
Copy link

I'm facing same problem. Have to call ChangeDetectorRef.detectChanges() from most backend requests/subscriptions.

Same code was working fine until update to v4+.

@montylab
Copy link
Author

montylab commented May 2, 2017

Thank you all, I'll close the ticket

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 11, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: core Issues related to the framework runtime
Projects
None yet
Development

No branches or pull requests

4 participants