$observe不在AngularJS工作
无法使$observe在更改时触发任何内容$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 !'
.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功能,但没有任何功能!