0

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. 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);
  }
}
10
  • can you post the uiModel$ content? Commented Aug 31, 2020 at 11:11
  • I can only see this happening if model.hasCreative = 'false', instead of model.hasCreative = false Commented Aug 31, 2020 at 11:18
  • uiModel$: Observable<UserInterfaceModel>; constructor(private modalService: NgbModal, private authService: AuthService, private userInterfaceService: UserInterfaceService) { } ngOnInit(): void { this.uiModel$ = this.userInterfaceService.userInterfaceModel$; } Commented Aug 31, 2020 at 11:44
  • export class UserInterfaceModel { hasCreative: boolean; } Commented Aug 31, 2020 at 11:44
  • @Nevnev can you update your answer with how you UserInterfaceModel instance looks like. Because it's obvious the model.hasCreative is resolving to true. So it's either a string, or something else, but definitely not false Commented Aug 31, 2020 at 11:59

1 Answer 1

1

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. If it dosnt work too, try render the value of model.hasCreative by adding somethin like this:

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

out of <ul> tag to see if model.hasCreative has true/false value or not.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks, i did try that and the span renders as true or false depending on what it is
is your problem fixed?
thanks yes. I changed the way i set isCreative and it worked.
model.hasCreative = isCreative.toLocaleLowerCase() === 'true' ? true : false;
So, it was a string. you said it's a boolean while it wasn't.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.