I have Angular Element Component (with Angular 8) which has 2 props. When I try to push something in the array the Shadow-Root doesn't re-render it(the counter yes instead). How I can force the rendering when i push an object in the component?
Thanks
This is the Component class
export class CounterComponent implements OnInit {
@Input()
counter: number;
@Input()
images: Array<any>;
constructor(private cdr: ChangeDetectorRef) { }
ngOnInit() {
this.counter = 0;
this.images = [];
}
add() {
this.counter++;
}
reduce() {
this.counter--;
}
@Input()
addImage() {
this.images.push({ ciccio: 'piccio'}); //no rerender
this.cdr.detectChanges(); // Error cdr is null
}
}
AppModule
import { BrowserModule, platformBrowser } from '@angular/platform-browser';
import { NgModule, Injector } from '@angular/core';
import { CounterComponent } from './counter/counter.component';
import { createCustomElement } from '@angular/elements';
@NgModule({
declarations: [
CounterComponent
],
imports: [
BrowserModule
],
providers: [],
entryComponents: [CounterComponent]
})
export class AppModule {
constructor(private injector: Injector) {
}
ngDoBootstrap( ) {
const el = createCustomElement(CounterComponent, {injector: this.injector});
customElements.define('count-component', el);
}
}
This is the component template
<div style="display: flex;justify-content: center;flex-direction: column">
<h1>Counter component</h1>
<h4>Count ---> {{counter}}</h4>
<button (click)="add()">Increase</button>
<button (click)="reduce()">Decrease</button>
</div>
<h3>
Images {{images.length}}
</h3>