How to view the property of an object?

I want to have an attribute directive that can be used as follows:

<div my-dir="{items:m.items, type:'0'}"></div>

In my directive, I can turn this into an object like:

function (scope, element, attributes, modelController)
{
   var ops = $.extend({}, scope.$eval(attributes.myDir));
}

Now let's say that m.items is an array. And somewhere along the line, ajax call replaces the array with a new one. I would like to keep track of this.

How can I look m.itemsor everything that was entered for the items property in the attribute? What should be the expression for the watch?

+4
source share
2 answers

You should be able to use scope. $ watch to view the attribute. Angular will call a callback with the entire object when any of the values ​​change.

function(scope, elem, attrs) {
  scope.$watch(attrs.myDir, function(val) {
    // Do something with the new value, val.
  }, true);
}

http://plnkr.co/edit/JBGqW8uFzh1eJ1Ppq3fT

, ( - ), html $.

: true , . 10 .

+6
scope.$watchCollection (watchExpression, listener)

: https://docs.angularjs.org/guide/scope

+1

All Articles