2

I have a component with an input and a variable:

@Input() data: string[];
@Input() val: string = '';

ngOnChanges(changes: SimpleChanges) {
    for (let propName in changes) {
       if (propName == 'data') {
            this.val = 'hello';
       }
    }
}

The problem is, i need to change the input val when data is changed. But i have the following error:

Expression has changed after it was checked. Previous value: 'false'. Current value: 'true'.

Thanks !

Trong Lam Phan
  • 2,292
  • 3
  • 24
  • 51

1 Answers1

0

It's easier to create a setter

_data: string[];
@Input() set data(val: string[]) {
  this._data = val;
  this.val = 'abc'
  // this.cdRef.detectChanges();
}
get data() { return this._data; }

@Input() val: string = '';

If you still get the error then add

constructor(private cdRef:ChangeDetectorRef) {}

and comment in the commented out line in the setter above.

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567