简体   繁体   English

Angular 未运行变更检测

[英]Angular is not running the change detection

I want to show or hide the button according to the value of boolean variable.我想根据 boolean 变量的值显示或隐藏按钮。 And for that i am using Output property.为此,我正在使用Output属性。 But when page is initialize button is hidden but when EventEmmiter emmits the value then the button is again hidden instead it has to be visible.但是当页面初始化时按钮被隐藏但是当 EventEmmiter 发出值时按钮再次被隐藏而不是它必须是可见的。

Parent Component.ts父组件.ts

showEditButton = false;

editButton(event: boolean) {
  this.showEditButton = true
}

Parent Component.html父组件.html

<app-tools-menu-items-open
  *ngSwitchCase="toolType.FILE_OPEN"
  [isTextEditor]="isText"
  (showEditButton)="editButton($event)"
></app-tools-menu-items-open>

// This component should be visible if showEditButton is true

<ng-container *ngIf="showEditButton">
  <app-tools-menu-items-pdf-edit
    *ngSwitchCase="toolType.EDIT_PDF"
  ></app-tools-menu-items-pdf-edit>
</ng-container>

Child Component.ts子组件.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);
    }
  }

Child Component.html子组件.html

 <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>

Here i see that editButton is called but Change detection in Angular is not working.在这里,我看到调用了 editButton,但 Angular 中的更改检测不起作用。

ngOnChanges Lifecycle run when previous value of the property is different from the current one. ngOnChanges生命周期在属性的先前值与当前值不同时运行。

 OnChanges(changes: any){
   if(changes.currentValue !== changes.previousValue){
    // white your logic here
  }

Read more about the ngDoCheck lifecycle hoot阅读更多关于ngDoCheck生命周期的信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM