简体   繁体   English

基于结果模型的具有异步管道内部条件的 Angular *ngIf

[英]Angular *ngIf with async pipe inner condtion based on the resulting model

I'd be a happy man if someone could explain why the following is not working as expected please?如果有人能解释为什么以下内容没有按预期工作,我会是一个快乐的人吗? The hasCreative is a boolean but regardless of its true/false value, the <li> is always displayed. hasCreative是一个布尔值,但不管它的真/假值如何,总是显示<li> Any suggestions would be great.任何建议都会很棒。 Thank you.谢谢你。

<ng-container *ngIf="uiModel$ | async as model">
    <ul class="nav" style="padding-bottom: 30px;">
      <li *ngIf="model.hasCreative" class="nav-item">
        <a class="nav-link active" routerLinkActive="active" [routerLink]="['']">Home</a>
     </li>
   </ul>
</ng-container>

export class UserInterfaceModel {
  hasCreative: boolean;
}

@Injectable({
  providedIn: 'root'
})
export class UserInterfaceService {
  user: CognitoUser;
  userLoggedIn = false;
  private userInterfaceModelSubject$: Subject<UserInterfaceModel> = new Subject();
  userInterfaceModel$ = this.userInterfaceModelSubject$.asObservable();

  constructor(private authService: AuthService) {
    combineLatest([this.authService.onUserLoaded$]).subscribe(([currentUser]) => {
      this.user = currentUser;
      this.userLoggedIn = true;
      this.buildUserInterfaceModel();
    });
  }

  buildUserInterfaceModel(){
    const model = new UserInterfaceModel();
    if (this.userLoggedIn && this.user !== null){
      model.hasCreative  = this.user.getSignInUserSession().getIdToken().payload.creative;
    }
    this.userInterfaceModelSubject$.next(model);
  }
}

Try using this:尝试使用这个:


<ng-container *ngIf="uiModel$ | async as model; else loading">
    <ul class="nav" style="padding-bottom: 30px;">
      <li *ngIf="model.hasCreative === true " class="nav-item">
        <a class="nav-link active" routerLinkActive="active" [routerLink]="['']">Home</a>
     </li>
   </ul>
</ng-container>

<ng-template #loading>
  Loading stuff...
</ng-template>

If loading template will render it means your Observable has no value.如果loading模板将呈现,则意味着您的Observable没有价值。 If it dosnt work too, try render the value of model.hasCreative by adding somethin like this:如果它也不起作用,请尝试通过添加如下model.hasCreative来渲染model.hasCreative的值:

<span>{{model.hasCreative}}<span>

out of <ul> tag to see if model.hasCreative has true/false value or not.<ul>标签中查看model.hasCreative是否具有true/false值。

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

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