0

HTML file:

<div [hidden]="isshow">
  <h1>this is a</h1>
</div>
<div [hidden]="!isshow">
 <h1>this is b</h1>
</div>

component.ts:

  public get isshow() {
    return (this._state === 'ready');
  }

The state value is changed by an EventListener, and will change from "connecting" to "ready".

What I want is to show "this is a" at the beginning then change to "this is b" when state is "ready", but looks it doesn't work. What should I do? Thanks in advance.

Dante Jiang
  • 177
  • 1
  • 13

4 Answers4

0

i think your display:none is override in your style use *ngIf instead

<div *ngIf="isshow">
Fateh Mohamed
  • 20,445
  • 5
  • 43
  • 52
0

Change your html to the following:

<div *ngIf="isshow">
  <h1>this is a</h1>
</div>
<div *ngIf="!isshow">
 <h1>this is b</h1>
</div>

You can refer to this answer to see the differences: https://stackoverflow.com/a/43034841/3329836

Jamie Rees
  • 7,973
  • 2
  • 45
  • 83
0

Just a piece of advice here. Due to some internal shenanigans angular does with it's change detection system it's better not to use getter for binding. Your

 public get isshow() {
    return (this._state === 'ready');
  }

will be called everytime your component checks for changes (just put console.log there before return (this._state === 'ready'); and you'll see)

it's better to use a pipe or just put the condtion this._state === 'ready' in your binding and this way it'll be called only when this._state actually changes

The same is also true for functions bound to your fields i.e you shouldn't use them in bindings

SebOlens
  • 519
  • 3
  • 15
-1

Try to add [hidden] { display: none; } in your stylesheets (remember it should be added globally). I'm not sure why, but it's kinda look like missing in Angular or there is some problems with it. I've found some old issues about it, but they are closed long time ago, but in plunkr it helped.

Patryk Brejdak
  • 1,571
  • 14
  • 26