RSS Amplifier

magarcia · Jul 4, 2016

$watch with Angular 2

0
Sign in to vote or save

magarcia

In my previous post, I was talking about how to implement events from Angular 1 in Angular 2. But in the snippet of code that I use as an example we can find another thing that not exists in Angular 2: $watch.

I start defining the problem. We can have a directive or Angular 1 component like that:

If we want to migrate this code to Angular 2 we find a trouble: the new Angular don't have scope, so it don't have $watch. How we can watch a directive attribute? The solution is the set syntax from ES6.

The set syntax binds an object property to a function to be called when there is an attempt to set that property.

So we can bind the input for a component to a function that does the same as the $watch function.

Read the original on magarcia.io

Comments

Nothing yet. Say the first thing.

    Sign in to join the conversation.