Angular 9 ngOnChange: SimplesChanges.firstChange is always false but the value changed

1.8k views Asked by At

I have this component. I want to do something just the first time the @Input changes. The problem is that the changes.firstChange is always false (on init and after changed).

@Component({
  changeDetection: ChangeDetectionStrategy.OnPush,
  selector: 'test',
  templateUrl: './t.component.html',
  styleUrls: ['./t.component.css']
})
export class TComponent implements OnChanges {

  @Input()
  myValue: string;

  ngOnChanges(changes: SimpleChanges) {
    console.log('myValue:  ', changes.myValue.isFirstChange()); // always false
    console.log('myValue:  ', changes);
  }

}

For using it, I'm doing:

  myValue = of('1').pipe(
    delay(2000),
    // startWith('2')
   )

I also tried:

  myValue = '1'

<test [myValue]="myValue | async "></test> // When using of

As you can see in the images changes.firstChange is always false, no matter if I pass a string or a new value later.

These are the outputs: enter image description here

enter image description here

And this is the output when using the rxjs value:(value changed but never changed firstChange to true

enter image description here

1

There are 1 answers

1
wissem chiha On

because that haven't previousValue