angular - 如何防止在 Angular 6 中评估 ngIf 条件时出现延迟?在评估值之前显示默认图像。如何防止这种情况发生?

标签 angular angular6 angular-ng-if

组件.ts

  this.canShowPrivateInfo;

  if (this.canEditDetails || this.loginId == this.loggedInUser) {                    
      this.canShowPrivateInfo = true;
  } else if (this.loggedInUserPrivacy) {
      this.canShowPrivateInfo = false;
  } else {
      this.canShowPrivateInfo =true;
   }

这是用于在显示 protected 图像和普通图像之间切换的条件 ( this.canShowPrivateInfo )

HTML

公共(public)图片

<img *ngIf="canShowPrivateInfo" 
     [src]="uploadedImageURL" 
      class="profile-pic"
 >

protected 图像

<span *ngIf="!canShowPrivateInfo" class="profile-pic">
  <i class="fas fa-user-lock profile-lock"></i>
</span>  

因此,即使 canShowPrivateInfo 为 true,它也会首先显示锁定图像,然后显示实际图像。

需要一秒钟的时间来评估然后更改。

如何防止图像最初在屏幕上闪烁?在图像之间切换而不闪烁的最佳方式是什么?

最佳答案

为了避免这种闪烁,您可以这样做:

  • 不要将 false 分配给 canShowPrivateInfo 作为时间 声明变量。
  • 然后在 HTML 中添加未定义检查。

例如:

TS:

public canShowPrivateInfo;

HTML:

<span *ngIf="!canShowPrivateInfo && canShowPrivateInfo !== undefined"
      class="profile-pic">
       <i class="fas fa-user-lock profile-lock"></i>
</span>

关于angular - 如何防止在 Angular 6 中评估 ngIf 条件时出现延迟?在评估值之前显示默认图像。如何防止这种情况发生?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56789169/

相关文章:

angular - 服务在 APP_INITIALIZER 之后实例化了两次

javascript - Angular Material 对话框预填充输入字段不验证

javascript - angularjs - ng-if 多重条件

javascript - 在 Angular View 中动态生成链接

javascript - 类型 'forEach' 上不存在属性 '{}'

angular - 如何检索 EventEmitter 响应 Angular 6

javascript - 带有 ng-if 的 Angular 指令似乎失去了模型

angularjs - 许多 ng-if 条件变得非常慢

Angular 2 : concat arrays in a pipe without loosing databinding

javascript - Angular 2 服务单例范围问题