Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect changes @Input binding in angular [duplicate]

Tags:

angular

I need to detect value changes in @Input binding angular and execute a function when the value is changed

@Component({
  selector: 'my-comp',
  ...
})
...
@Input() myValue

//detect myValue changes

<my-comp [myValue]= "val"></my-comp>

I need to execute some code component class when val changes.

like image 773
san gallage Avatar asked Oct 17 '25 08:10

san gallage


1 Answers

You could simply use set here, like this:

_myvalue: any;
@Input() set myValue(value: any) {
    ... // Your code goes here
    this._myvalue = value;
}

Now, everytime you assign a value to myValue inside your template the setter is called and your code will be executed.

I hope that helps!

like image 72
nullchimp Avatar answered Oct 19 '25 01:10

nullchimp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!