Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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>
share|improve this question
    
If the change is made outside of angular (for example, by Bootstrap), angular won't know about it. The $observe is not evaluated until the next digest cycle. –  Anthony Chu Apr 27 at 1:01
    
@AnthonyChu how can I detect it still? This has been killing me! –  Mohamed El Mahallawy Apr 27 at 2:44

1 Answer 1

up vote 1 down vote accepted

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.

share|improve this answer
    
I included the $watch function and nothing is working! –  Mohamed El Mahallawy Apr 27 at 2:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.