Tiny Grades with Angular 2:@Input set function

Making a Set Function

While working on a project at work, I wanted to execute a function every time an Input on a component changed. Lots of Googling turned up nothing but I eventually stumbled onto an answer. Here it is.

Ahh found it: While looking for an unrelated item on angular 2 page, I found a bit of documentation detailing the above. You can find it here

Solution

You have a component that takes in an Input like so. 

Normally you’d probably just use the variable as it. But, what if you want to execute a function when it changed? All you have to do is create a Set function. 

Now anytime the data attribute is set, the function will execute. 

Wrap-Up

There ya have it. It took me a long time to find the solution so hopefully this will help you so you didn’t have to! 

Thanks for reading!

 

Facebooktwittergoogle_plusredditpinterestlinkedinby feather