I'm trying to insert HTML Input Element on button click inside div container. I have created a directive & added the directive on the child input elements. Whenever I click on the input element, that element should be removed from the DOM.
I'm able to achieve this behavior but whenever I click on one HTML Input Element, then all the HTML Elements are removed from the DOM which is not expected.
So, How to get single html input elementref in directive ? app.component.html
<div class="btn-container">
<button (click)="addInputElement()">Add Input</button>
</div>
<div class="box-container">
<input deleteDir *ngFor="let el of inputEl; let i = index" id={{i}} [value]=el.value>{{el.value}}/>
</div>
app.component.ts
export class AppComponent implements OnInit{
inputEl = [];
addInputElement() {
this.inputEl.push({value: Math.random()});
}
}
app.directive.ts
constructor(private elementRef: ElementRef,
private renderer: Renderer2) { }
@HostListener('click') onClick() {
this.renderer.removeChild(this.elementRef.nativeElement.parentNode,this.elementRef.nativeElement)
}