我的角度应用程序中有一个自定义结构指令,如下所示:
@Directive({
selector: '[appIfData]'
})
export class IfDataDirective {
private hasView = false;
@Input()
set appIfData(condition: boolean) {
if (condition && !this.hasView) {
this.viewContainerRef.clear();
this.viewContainerRef.createEmbeddedView(this.templateRef);
this.hasView = true;
} else if (!condition) {
this.viewContainerRef.clear();
const factory = this.componentFactoryResolver.resolveComponentFactory(ContentMessageComponent);
const messageComponentRef = this.viewContainerRef.createComponent(factory);
messageComponentRef.instance.message = 'No data is available yet';
messageComponentRef.instance.icon = 'fas fa-info';
this.hasView = false;
}
}
}
在html模板中使用它:
<ng-container *appIfData="(referenceService.documentUserTypes$ | async) as userDocTypes">
但是我无法在模板的其余部分访问声明的变量'userDocTypes',就像我在使用ngIf
时所做的那样。
我想这是一种正常的行为,但我找不到一个很好的方法来做到这一点。
任何帮助将不胜感激。
编辑:
这就是我使用它的方式,它是一个子元素。如前所述,如果我将其更改为* ngIf,它可以正常工作:
编辑2:
更新指令
@Directive({
selector: '[appIfData]'
})
export class IfDataDirective {
private hasView = false;
@Input()
set appIfData(data: any) {
if (data && !this.hasView) {
this.viewContainerRef.clear();
this.viewContainerRef.createEmbeddedView(this.templateRef, { appIfData: data });
this.hasView = true;
} else if (!data) {
this.viewContainerRef.clear();
const factory = this.componentFactoryResolver.resolveComponentFactory(ContentMessageComponent);
const messageComponentRef = this.viewContainerRef.createComponent(factory);
messageComponentRef.instance.message = 'No data is available yet';
messageComponentRef.instance.icon = 'fas fa-info';
this.hasView = false;
}
}
你可以像这样向createEmbeddedView
添加上下文
this.viewContainerRef.createEmbeddedView(this.templateRef, {appIfData: condition});
另请注意,condition
在您的示例中不是布尔值,它可能在aot编译期间崩溃。将条件类型更改为其他内容
在as
中声明的变量只能在其子元素中访问
<ng-container *appIfData="(referenceService.documentUserTypes$ | async) as userDocTypes">
<div>{{ userDocTypes }}</div> // you can access here
</ng-container>
<div>{{ userDocTypes }}</div> // you cannot access here
我认为你对template reference
感到困惑,可以在模板中访问它
<div>
<input type="text" #myInput value="123">
</div>
<div>{{ myInput.value }}</div> // you can access template variable outside of it
在他的回答中看到安德烈的评论后,我正在使用这个:
/**
* Cache for document user types
*/
public documentUserTypesCache$: BehaviorSubject<DocumentUserType[]>;
/**
* Get document user types
*/
public get documentUserTypes$(): BehaviorSubject<DocumentUserType[]> {
if (!this.documentUserTypesCache$) {
this.documentUserTypesCache$ = new BehaviorSubject<DocumentUserType[]>([]);
this.getDocumentUserTypes().pipe(tap(r => this.documentUserTypesCache$.next(r))).subscribe();
}
return this.documentUserTypesCache$;
}
并改为:
/**
* Cache for document user types
*/
public documentUserTypesCache$: BehaviorSubject<DocumentUserType[]>;
/**
* Get document user types
*/
public get documentUserTypes(): DocumentUserType[] {
if (!this.documentUserTypesCache$) {
this.documentUserTypesCache$ = new BehaviorSubject<DocumentUserType[]>(null);
this.getDocumentUserTypes().pipe(
filter(r => r && r != null),
tap(r => this.documentUserTypesCache$.next(r))).subscribe();
}
return this.documentUserTypesCache$.getValue();
}
不是最好的,但解决方法的工作原理