我尝试了这样的方法:
@ViewChild('draggable') private draggableElement: ElementRef;
this.draggableElement.nativeElement.remove();
HTML:
<div #draggable>Block</div>
你的代码也可以工作,你所要做的就是删除元素OnInit(),如果您尝试删除构造函数上的元素,则视图尚未准备好。
这是一个例子
in .html
<div #draggable>Block</div>
and in .ts
export class AppComponent implements OnInit {
name="Angular";
@ViewChild('draggable') private draggableElement: ElementRef;
constructor() { }
ngOnInit() {
this.draggableElement.nativeElement.remove();
}
}
这是一个斯塔克闪电战 https://stackblitz.com/edit/angular-renderer2-remove-pucs2c?file=app%2Fapp.component.ts demo
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)