Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
$observe不在AngularJS工作_Angularjs_Angularjs Directive_Angularjs Scope_Angular Ui - Fatal编程技术网

$observe不在AngularJS工作

$observe不在AngularJS工作,angularjs,angularjs-directive,angularjs-scope,angular-ui,Angularjs,Angularjs Directive,Angularjs Scope,Angular Ui,无法使$observe在更改时触发任何内容 .directive('watchContent', function () { return { restrict: 'A', link: function (scope, elem, attrs) { console.log(attrs.class) attrs.$observe("class", function(value){ console.log('change !'

无法使$observe在更改时触发任何内容

.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')
            }
        });
    }
};
})
这里是我试图触发的地方:

  <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>
  • 内容选项
    • {{option.slug}

  • 我认为这是因为类值没有插值

    $observe仅适用于诸如class=“{{whatever}}”之类的插值属性


    由于您的类不是插值的,因此您需要使用scope.$watch(…)。

    如果更改是在angular之外进行的(例如,通过引导),angular将不知道它。$observe直到下一个摘要周期才被评估。@AnthonyChu我怎么还能检测到它呢?这真让我受不了!我包括$watch功能,但没有任何功能!