angular - 指令中的 EventEmitter 不在 Parent 中处理
问题描述
我创建了一个指令,根据用户声明显示或隐藏组件。
is-allowed.directive.ts
import { Directive, TemplateRef, ViewContainerRef, Input, Output, EventEmitter } from '@angular/core';
import { SecurityService } from 'app/_security/security.service';
@Directive({
selector: '[appIsAllowed]'
})
export class IsAllowedDirective {
@Output() isReady = new EventEmitter<boolean>();
constructor(
private templateRef: TemplateRef<any>,
private viewContainer: ViewContainerRef,
private ss: SecurityService,
) { }
@Input('appIsAllowed') set isAllowed(claimType: any) {
if (this.ss.hasClaim(claimType)) {
this.viewContainer.createEmbeddedView(this.templateRef);
this.notAllowed(false);
} else {
this.notAllowed(false);
this.viewContainer.clear();
}
}
notAllowed(isAllowed: boolean) {
debugger;
this.isReady.emit(isAllowed);
}
}
然后我有2个组件。1 是管理模板组件,另一个是示例组件。
admin-template.component.html
<ng-container *ngIf="sampleToUpdate">
<app-sample *appIsAllowed="'Admin'" (isReady)="notifyIfAllowed($event)" [sampleData]="sampleToUpdate"></app-sample>
</ng-container>
admin-template.component.ts
notifyIfAllowed($event: boolean) {
debugger;
if (!$event) {
this.toastr.error('You are not allowed to do this operation', 'Unauthorized', {
closeButton: true,
progressBar: true,
timeOut: 3000
});
}
}
样本.component.ts
@Input() sampleData: SampleData;
constructor() { }
ngOnInit(): void {
console.log(this.sampleData);
}
一切似乎都按指令中的预期工作,但不知何故,父组件notifyIfAllowed()
没有被调用,因为它没有进入断点。我想要实现的是;什么时候<app-sample>
无法显示,因为claims
在directive
我需要一个 toastr 来通知用户。
解决方案
推荐阅读
- javascript - Fabric js:填充和描边重叠,看起来好像元素有2个描边
- swift - 更新 BehaviorRelay 属性的值
- android - 从反应原生的android模拟器连接到模拟器上运行的云功能
- wordpress - 在 GSC for Wordpress 站点中将多个图像 URL 作为单个 URL 获取
- android - Android Studio 不应用代码更改
- java - ISO-8859-1 字符编码在 Linux 中不起作用
- oracle - 如何在 oracle 索引上创建触发器
- android - 如何在颤动中将抽屉链接到tabBar
- php - 如何使用php链接目录html文件中的引导文件
- javascript - 有没有办法访问/修改节点模块/功能的“执行上下文”