Angular fidn html信息,带角度指令
我想用指令查找元素信息。 我把这个指令放在标签上,我需要获得元素信息,如Angular fidn html信息,带角度指令,angular,typescript,directive,Angular,Typescript,Directive,我想用指令查找元素信息。 我把这个指令放在标签上,我需要获得元素信息,如宽度和顶部和 现在我在指令中编写以下代码: loadingFactory: ComponentFactory<LoadingComponent>; loadingComponent: ComponentRef<LoadingComponent>; @Input() set SnipperLoading(data: Promise<any>) { // this.
宽度
和顶部
和
现在我在指令中编写以下代码:
loadingFactory: ComponentFactory<LoadingComponent>;
loadingComponent: ComponentRef<LoadingComponent>;
@Input() set SnipperLoading(data: Promise<any>) {
// this.containerRef.clear();
this.containerRef.createEmbeddedView(this.templateRef);
this.loadingComponent = this.containerRef.createComponent(this.loadingFactory);
data.then(() => {
this.containerRef.clear();
this.containerRef.createEmbeddedView(this.templateRef);
});
}
constructor(
private el: ElementRef,
private containerRef: ViewContainerRef,
private templateRef: TemplateRef<any>,
private componentFactoryResolver: ComponentFactoryResolver
) {
this.loadingFactory = this.componentFactoryResolver.resolveComponentFactory(LoadingComponent);
let myCurrentContent: string = el.nativeElement.innerHTML; // get the content of your element
el.nativeElement.innerHTML = 'my new content'; // set content of your element
}
.如何访问angular directive中的元素信息?您的打字是故意的吗?(
width
vswith
)@PhilippMeissner无人。我修正了我实际上指的是你提供的代码。我建议不要把逻辑放在指令的构造函数中。请改用Angular的生命周期挂钩,不要忘记,在尝试访问视图中的元素之前,可能需要使用ngAfterViewInit来确保视图存在。
<label *SnipperLoading="user$"> SnipperLoading </label>
console.log(el.nativeElement.with)