我的目标是当一个特定的指令接收类“重点”,通过纳克级的做一些动作。 但是,看到了在每个指令的变化似乎是在做奇怪的事情我不明白。 下面是代码第一:
http://plnkr.co/edit/6Te8O2mlBI058DpwGF7f?p=preview
的index.html
<test-question
ng-class="{'focused': tracer.focus == 1}"
focusnum = "{{n}}"
ng-click="tracer.focus = 1"
>
Test Question 1
</test-question>
<test-question
ng-class="{'focused': tracer.focus == 2}"
focusnum = "{{n}}"
ng-click="tracer.focus = 2"
>
Test Question 2
</test-question>
<test-question
ng-class="{'focused': tracer.focus == 3}"
focusnum = "{{n}}"
ng-click="tracer.focus = 3"
>
Test Question 3
</test-question>
的script.js
angular.module('sampleApp', [])
.controller("SampleController", function($scope) {
$scope.tracer = {
focus: 1
}
})
.directive('testQuestion', function() {
return {
restrict: 'E',
link: function($scope, el, attrs) {
$scope.$watch(function() {
return el.attr('class');
}, function(newValue, oldValue) {
// do something here when directive gets class focused
console.log("text", el.text());
console.log("newValue", newValue);
console.log("oldValue", oldValue);
});
}
}
});
最初,第一测试问题被突出显示。 当您单击第二个问题,我希望控制台登录
"text" Test Question 1 "newValue" undefined "oldValue" focused "text" Test Question 2 "newValue" focused "oldValue" undefined
然而,实际上控制台登录
"text" Test Question 1 "newValue" focused "oldValue" undefined
其结果是,这种怪异的行为我不能准确地当指令接收集中的类检测。 它看起来好像手表是一个周期的后期。 这将是巨大的,如果一个老乡朋友可以帮助我了解为什么发生这种情况。 谢谢!
额外
之后我点击的问题2,如果我随后单击问题3控制台日志
"text" Test Question 1 "newValue" "oldValue" focused "text" Test Question 2 "newValue" focused "oldValue" undefined