Question

Cannot get the $observe to trigger anything on change.

.directive('watchContent', function () {
return {
    restrict: 'A',
    link: function (scope, elem, attrs) {
        console.log(attrs.class)

        attrs.$observe("class", function(value){
            console.log('change !');
            if(value.indexOf("open") > -1){
                alert("yes")
            }
        })
        scope.$watch(function() {return attrs.class; }, function(newValue, oldValue, scope){
            console.log('change !');
            console.log(newValue)
            console.log(oldValue)
            console.log(scope)
            console.log(attrs.class)
            // debugger;
            if (elem.hasClass('open')) {
              console.log('YEs')
            } else {
              console.log('no')
            }
        });
    }
};
})

Here is where I am trying to trigger:

  <li class="dropdown dropdownChange" id="contentMenu" watch-content>
          <a class="dropdown-toggle" >
            Content options
          </a>
          <ul class="dropdown-menu" >
            <li ng-repeat="option in options">
              <a><input type="checkbox" checklist-model="contentChoices"  checklist-value="option.id" ng-click="$event.stopPropagation()" ng-change="changed()"> {{option.slug}} </a>
            </li>
          </ul>
        </li>
Was it helpful?

Solution

I think it's because the class value you have which is not interpolated.

$observe only works for interpolated attributes such as class="{{whatever}}".

Since your class is not interpolated, therefore you need to use scope.$watch(...) instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top