Angular未运行更改检测
我想根据布尔变量的值显示或隐藏按钮。为此,我使用了输出属性。但当页面处于初始化状态时,按钮隐藏,但当EventEmmiter显示该值时,按钮再次隐藏,而不是必须可见 父组件.tsAngular未运行更改检测,angular,typescript,angular11,Angular,Typescript,Angular11,我想根据布尔变量的值显示或隐藏按钮。为此,我使用了输出属性。但当页面处于初始化状态时,按钮隐藏,但当EventEmmiter显示该值时,按钮再次隐藏,而不是必须可见 父组件.ts @Output('showEditButton') showEditButton = new EventEmitter<boolean>(); uploadFile(event: Event): void { this.loaderService.shouldLoad(true); c
@Output('showEditButton') showEditButton = new EventEmitter<boolean>();
uploadFile(event: Event): void {
this.loaderService.shouldLoad(true);
const files = (event.target as HTMLInputElement).files;
if (files && files.length > 0) {
const fileType = files[0].name.substring(
files[0].name.lastIndexOf('.') + 1
);
this.redirectTo(fileType, {
type: TOOL_TYPE.FILE_OPEN,
action: 'file',
value: {
name: files[0].name,
blob: files[0] as Blob,
status: 'selected',
} as FileResource,
});
this.loaderService.shouldLoad(false);
// Emiting the value
this.showEditButton.emit(true);
} else {
// TODO: Handle improper selection.
this.loaderService.shouldLoad(false);
}
}
父组件.html
//如果showEditButton为true,则此组件应可见
子组件.ts
@Output('showEditButton') showEditButton = new EventEmitter<boolean>();
uploadFile(event: Event): void {
this.loaderService.shouldLoad(true);
const files = (event.target as HTMLInputElement).files;
if (files && files.length > 0) {
const fileType = files[0].name.substring(
files[0].name.lastIndexOf('.') + 1
);
this.redirectTo(fileType, {
type: TOOL_TYPE.FILE_OPEN,
action: 'file',
value: {
name: files[0].name,
blob: files[0] as Blob,
status: 'selected',
} as FileResource,
});
this.loaderService.shouldLoad(false);
// Emiting the value
this.showEditButton.emit(true);
} else {
// TODO: Handle improper selection.
this.loaderService.shouldLoad(false);
}
}
@Output('showEditButton')showEditButton=neweventemitter();
上载文件(事件:事件):无效{
this.loaderService.shouldLoad(true);
const files=(event.target作为HTMLInputElement).files;
如果(files&&files.length>0){
const fileType=files[0]。name.substring(
文件[0]。名称。lastIndexOf('.')+1
);
重定向到(文件类型{
类型:工具类型。文件打开,
操作:“文件”,
价值:{
名称:文件[0]。名称,
blob:文件[0]作为blob,
状态:“已选定”,
}作为文件资源,
});
this.loaderService.shouldLoad(false);
//释放价值
此.showEditButton.emit(true);
}否则{
//TODO:处理不正确的选择。
this.loaderService.shouldLoad(false);
}
}
子组件.html
{{“translateMyDocuments”| translate}
{{“translateOneDrive”| translate}
{{“translategogledrive”| translate}
在这里,我看到editButton被调用,但Angular中的更改检测不起作用。您确定
editButton
被调用了吗?@Moshezauros是的,我使用console.log()进行了检查显示子组件模板是否可以创建stackblitz?this.showEditButton=true
应该是父组件中的this.showEditButton=event
。
@Output('showEditButton') showEditButton = new EventEmitter<boolean>();
uploadFile(event: Event): void {
this.loaderService.shouldLoad(true);
const files = (event.target as HTMLInputElement).files;
if (files && files.length > 0) {
const fileType = files[0].name.substring(
files[0].name.lastIndexOf('.') + 1
);
this.redirectTo(fileType, {
type: TOOL_TYPE.FILE_OPEN,
action: 'file',
value: {
name: files[0].name,
blob: files[0] as Blob,
status: 'selected',
} as FileResource,
});
this.loaderService.shouldLoad(false);
// Emiting the value
this.showEditButton.emit(true);
} else {
// TODO: Handle improper selection.
this.loaderService.shouldLoad(false);
}
}
<div>
<button
mat-icon-button
[matMenuTriggerFor]="options"
(menuOpened)="setToolState(true)"
(menuClosed)="setToolState(false)"
class="icon-btn"
[ngClass]="{ active: isActive }"
matTooltip="{{ 'translateOpen' | translate }}"
>
<mat-icon svgIcon="file" class="app-icon-hover"></mat-icon>
</button>
<mat-menu #options="matMenu">
<button mat-menu-item (click)="openMyDocuments()">
<mat-icon svgIcon="local_drive" class="app-icon-hover"></mat-icon>
{{ "translateMyDocuments" | translate }}
</button>
<input
type="file"
#fileInput
(change)="uploadFile($event)"
accept=".pdf, .txt, .text, .epub"
/>
<button mat-menu-item (click)="openOneDrive()" *ngIf="envName !== 'prod'">
<mat-icon svgIcon="one_drive" class="app-icon-hover"></mat-icon>
{{ "translateOneDrive" | translate }}
</button>
<button mat-menu-item (click)="openGoogleDrive()" *ngIf="envName !== 'prod'">
<mat-icon svgIcon="google_drive" class="app-icon-hover"></mat-icon>
{{ "translateGoogleDrive" | translate }}
</button>
</mat-menu>
</div>
<div #fakeHld id="fakeHld"></div>