Here I'm trying to study how change detection work. having array(children) in parent component and in children using change detection OnPush. Trying to change the array by splice or push is still affecting the child component template. How come this works, even though the reference of the @Input does not change?
my parent component.ts
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit, AfterViewInit {
children = [1,2,3,4,5,6,7,8,9]
emit(j) {
this.children.splice(j, 1);
}
}
parent html
<div>
<app-multi [input]="children" (output)="emit($event)"></app-multi>
</div>
my child component.ts
@Component({
selector: 'app-multi',
templateUrl: './multi.component.html',
styleUrls: ['./multi.component.css'],
changeDetection:ChangeDetectionStrategy.OnPush
})
export class MultiComponent implements OnInit,OnChanges,DoCheck,AfterViewInit{
@Input('input') ele = [];
data = [];
@Output('output') outputEmi = new EventEmitter();
ngOnInit() {
this.data = this.ele;
}
clicks(value){
this.outputEmi.emit(value);
}
}
child html
{{name.name}}
<table>
<tbody>
<tr>
<td *ngFor="let head of data; let i = index;">
<span>{{head}}</span>
<button class="delete" (click)="clicks(i)">x</button>
</td>
</tr>
</tbody>
</table>