我使用的是工具提示指令,所以当我点击<button>
时 - 我动态插入并显示工具提示。Angular - 如何删除所有其他现有指令?
它工作得很好,我也看到了提示:
这是我点击一个按钮时,用来注入工具提示代码:
@Directive({ selector: '[popover]'})
class Popover {
private _component: ComponentRef<>;
constructor(private _vcRef: ViewContainerRef, private _cfResolver: ComponentFactoryResolver,private elementRef: ElementRef) {
}
@HostListener('click')
toggle() {
if (!this._component) {
const componentFactory = this._cfResolver.resolveComponentFactory(PopoverWindow);
this._component = this._vcRef.createComponent(componentFactory);
} else {
this._vcRef.clear()
this._component.destroy();
this._component = null;
}
}
}
但我不要想让多个工具提示出现在屏幕上。
换句话说,在我注入工具提示之前 - 我想删除所有现有的工具提示 - 如果有的话。
问:
我怎么能“找到”所有现有的提示和删除他们之前,我插入一个新的?
我知道我可以给每一个类添加一个类,然后通过removeNode删除它们,但是我想以角方式来实现它。
顺便说一句 - 我会很高兴找到一个通用的解决方案也为组件和不仅为指令。 ( 如果可能的话)。
https://开头plnkr。 co/edit/NU2FKlO6Q66bRde9m9AC?p = preview这里有一个更多的方法https://medium.com/@amcdnl/building-tooltips-for-angular2-396320fa938f检查此问题https://stackoverflow.com/questions/42598169/外接AC omponent动态地对一胎化元素使用-A-指令。 – yurzui
:-) @yurzui一如既往,谢谢。 –
@yurzui你可以请看看[这里](https://stackoverflow.com/questions/44530654/angular-service-injecting-dynamic-component)? –