0

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 Answers1

0

because that haven't previousValue

  • at some point there's a first change. It starts with null and then it has a string value. Even if the `startWith` is enabled, the problem is the same. or just passing a string too. – Diego Ramirez Oct 09 '20 at 14:17