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.

Dynamically created component model is not being reflected in the template on change

See original GitHub issue

🐞 bug report

Affected Package

The issue is caused by package 'not sure'

Is this a regression?

No

Description

Dynamically created component model is not being bind to the view on model change. Reproduced the issue in below stackblitz project.

πŸ”¬ Minimal Reproduction

https://stackblitz.com/edit/angular-tyhfku

🌍 Your Environment

Angular Version:


@angular/common7.2.5
@angular/compiler7.2.5
@angular/core7.2.5
@angular/forms7.2.5
@angular/platform-browser7.2.5
@angular/platform-browser-dynamic7.2.5
@angular/router7.2.5
core-js2.6.5
rxjs6.4.0
zone.js0.8.29




Anything else relevant?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
alexzuzacommented, May 17, 2019

No, it shouldn’t since config.active = true and view.oldValue[i] is also true so component will never receive updated value. What you can do it to use two-way binding and don’t mutate state. https://stackblitz.com/edit/angular-5qansx?file=src/app/hero.component.ts

1reaction
alexzuzacommented, May 17, 2019

@azimsodikov I suspect you’re expecting that after you changed the active property by clicking on the line inside app-section component and then clicked on the Restore State button it should revert your primitive value to the default value, right?

The first we can try is to to remove dynamic loading https://stackblitz.com/edit/angular-zsphvy?file=src%2Fapp%2Fsection.component.ts

As we can the dynamic component is not a problem here.

Now, let’s think when ngOnChanges should be called.

It should be called when Angular (re)sets data-bound input properties.

parent component

const config= { foo: false };

<app-section [active]="config.foo"></app-section>

child component

@Component({
  selector: 'app-section',
  template: `<h1 (click)="active = !active" >click me to change state</h1>`,
})
export class SectionComponent implements OnChanges  {
  @Input() active: boolean;

  ngOnChanges() { 
    /* no changes */
    console.log(this.active);
  }
}

When Angular updates property it sets value to that property and also updates view.oldValue to keep track changes.

  • When does Angular update an input property?
  • When there is a change or in other words when view.oldValue is not identical to current value.

The first time the ngOnChanges is called is when we set default value so it changes from undefined to false.

Note that we deal with primitive value here. So we have AppSectionInstance.active = false; view.oldValue = false;

Now you click on the line and change AppSectionInstance.active to false. view.oldValue remains the same. And when you’re trying to update config Angular can’t recognize the change since new value(false) is not changed(view.oldValue = false)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Detect data changes on dynamic component is not working
Its not possible for a dynamically created component to be aware of changes. What worked for me was triggering change detection at the...
Read more >
Loading Components Dynamically in an Angular App
A tutorial that shows how to create an Angular app with dynamic component loading.
Read more >
Dynamic component loader - Angular
Component templates are not always fixed. An application might need to load new components at runtime. This cookbook shows you how to add...
Read more >
Form validation of dynamically created input elements does ...
When you create a simple form with two text input elements being "required" and the second input is created by a dynamic sub...
Read more >
Dynamically-rendered ASP.NET Core Razor components
The {COMPONENT} placeholder is the dynamically-created component type. IRefreshable is an example interface provided by the developer forΒ ...
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