最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular 2 not re-rendering until I click the page? - Stack Overflow

programmeradmin1浏览0评论

On this page here.

It's possible to see that the Facebook login button is initially disabled. (Using [disabled]="myProperty")

Once an API request /facebooklogin/isnewuser returns, it is enabled.

Everything works fine. However, the button won't be re-rendered until I click on the page.

I've also noticed that this happens with most things involving the data-binder in Angular.

  • Is this intended behavior?
  • Is there anyway around this?
  • Is this specific to Angular?

Tested on Firefox & Chrome

On this page here.

It's possible to see that the Facebook login button is initially disabled. (Using [disabled]="myProperty")

Once an API request /facebooklogin/isnewuser returns, it is enabled.

Everything works fine. However, the button won't be re-rendered until I click on the page.

I've also noticed that this happens with most things involving the data-binder in Angular.

  • Is this intended behavior?
  • Is there anyway around this?
  • Is this specific to Angular?

Tested on Firefox & Chrome

Share Improve this question asked Feb 1, 2017 at 21:33 williamsandonzwilliamsandonz 16.5k23 gold badges107 silver badges190 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 10

It seems that for some reason change detection isn't happening as it should. You can "force" it with NgZone or manually trigger change detection with ChangeDetectorRef:

import { ChangeDetectorRef } from '@angular/core' 

constructor(private ref: ChangeDetectorRef) {}

and then run detectChanges() after setting your variable:

// whatever you are using to disable/enable button
this.myProperty = false;
this.ref.detectChanges();

or the use of NgZone:

import { NgZone } from '@angular/core' 

constructor(private ngZone: NgZone) {}

and add something like the following where you are enabling the button:

// whatever you are using to disable/enable button
this.ngZone.run(() => {this.myProperty = false})

I can't say anything more to as why this is happening. I have noticed that once in while these things happen for no apparent reason, this seems to be one of those cases. If anyone has some insight on this, please do tell. Haven't e upon anyone who has been able to explain this, yet :)

发布评论

评论列表(0)

  1. 暂无评论