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.

upgradeAdapter Change detection issue when emitting events from ngOnInit or object construction.

See original GitHub issue

I’m submitting a … (check one with “x”)

[X ] bug report => search github for a similar issue or PR before submitting
[ ] feature request
[ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior If you emit an event from a child component ngOnInit, then set a flag on the parent as a result of that event and use an ngIf to evaluate that flag, the template is unable to render the *ngIf element. As a secondary bug, if you’re using upgradeAdapter, an exception is swallowed. If you’re not using the upgradeAdapter, the following error occurs: Error in ./FeatureComponent class FeatureComponent - inline template:16:45 caused by: Expression has changed after it was checked. Previous value: ‘undefined’. Current value: ‘true’.

Expected behavior Should be able to emit events in ngOnInit, or it should be documented that you can’t do this. A meaningful exception should be thrown whether you’re using the upgradeAdapter or not.

Minimal reproduction of the problem with instructions http://plnkr.co/edit/mspxruqKzHHnuuzs4z0M?p=preview

Run the plnkr. Click on Hardcoded Employee. Notice when we don’t use *ngIf, it works, when we do, the *ngIf isn’t evaluated.

Change the option to Option 3. Click on Http Employee. Notice how the *ngIf fails again.

Now go to main.ts Comment the following code: angular.module(‘myApp’) .directive(‘root’, <IDirectiveFactory>upgradeAdapter.downgradeNg2Component(Root));

upgradeAdapter.bootstrap(document.documentElement, [‘myApp’], { strictDi: true });

Uncomment the following code: platformBrowserDynamic().bootstrapModule(AppModule);

Now repeat the steps above. Same behavior but you will see template exceptions.

What is the motivation / use case for changing the behavior? Change detection fails in what could be a common occurance. This is very difficult to troubleshoot and took a very long time to find the root cause.

Please tell us about your environment: Plnkr

  • Angular version: 2.0.X 2.1.2

  • Browser: [all | Chrome XX | Firefox XX | IE XX | Safari XX | Mobile Chrome XX | Android X.X Web Browser | iOS XX Safari | iOS XX UIWebView | iOS XX WKWebView ] all

  • Language: [all | TypeScript X.X | ES6/7 | ES5] all

  • Node (for AoT issues): node --version =
    na

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:6
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
nodehackcommented, Dec 7, 2016

This problem occurs with something as simple as using an *ngIf to toggle 2 components.

Here’s a plunker showing a very simple implementation.
http://plnkr.co/edit/qSPoI9IHbUQKocc8ibwb?p=preview (thanks ryanlangton for the template)

You’ll see that as soon as you click the toggle button to toggle the component, the angular zone gets lost.

A workaround is to use NgZone’s run function to put you back into angular’s zone. This seems really silly though. I would expect all ng2 component lifecycle events should happen within angular’s zone.

0reactions
angular-automatic-lock-bot[bot]commented, Sep 11, 2019

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Change detection not triggered on some event - Stack Overflow
When the observable emits its next value some change happen in my component. The strange thing is that the component will or will...
Read more >
Chapter 9. Change detection and component lifecycle
In this chapter, we'll discuss the change detection (CD) mechanism that monitors the asynchronous events of your app and decides whether the UI...
Read more >
Lifecycle hooks - Angular
The lifecycle continues with change detection, as Angular checks to see when data-bound properties change, and updates both the view and the component ......
Read more >
angular/angular - Gitter
I forked angular-2-local-storage and switched the constructor to take ... I set input value through jquery but change isn't being detected.
Read more >
Rangle.io : Angular 2 Training - Sathyalog
The class constructor is called when an object is created using the new ... In Angular 2, data and event change detection happens...
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