Search code examples
angularjsangularjs-directiveng-animate

Can`t set up $watch of attribute


I was creating simple example of angular animation based on element,class and attribute directive. http://plnkr.co/edit/Tw2dAGQJqoUy2sOOdoJr?p=preview

I`ve set up a variable and animate it change

<body ng-init="myValue=0">

In case of attribute, i tried to watch not for the variable, but for the attribute

In html:

<div my-dir3 = {{myValue}}></div>

In script.js:

scope.$watch(attr.myDir3, function(newVal) {...}

But it doesn`t works(except page openning).

Can you give me explanation, why? Or maybe some links for explaining the question?


Solution

  • You can do it, but you should use $observe instead like :

    attr.$observe('myDir3', function(val) {
        //change
    });
    

    Here is your updated Plunkr