1
我正在创建一个活动websocket应用程序,它需要在更改值时抓住用户的注意力。
以下指令在多个地方使用的同一页上:
<span class="badge" animate-on-change animate-class="highlightWarning" animate-watch="totalAnswers">{{totalAnswers}}</span>
和
<div animate-on-change animate-class="colorWarning" animate-watch="rq.answer"
ng-switch="question.type"
ng-repeat="rq in recentResponse.answers"
ng-if="rq.question == question.id">
现在,rq.answer很少改变,但每一次变化的实际值的变化。 IE:它工作正常。
不幸的是,第一次在第一次调用后不工作(在页面加载时),但{{totalAnswers}}显示了视觉改变。该模型肯定会更新,但$ watch不会被调用。为什么是这样?
.directive('animateOnChange', function ($timeout) {
return {
scope: {
animateClass: '@',
animateTime: '@',
animateWatch: '@',
animateCollection: '@'
},
link: function (scope, element, attr) {
function call() {
element.addClass(scope.animateClass);
$timeout(function() {
element.removeClass(scope.animateClass);
}, scope.animateTime || 1000); // Could be enhanced to take duration as a parameter
}
if (scope.animateWatch)
scope.$watch(scope.animateWatch, function (nv, ov) {
call();
}, true);
if (scope.animateCollection)
scope.$watchCollection(scope.animateCollection, function (nv, ov) {
call();
});
}
};
})
;
我试过这个,不幸的是这并没有解决我的问题,因为Watch的第一个参数也可以带一个String。无论这种改变已经阻止了TotalAnswers完全工作。 –
@ThomasNairn为什么你用'@'而不是'='绑定? –
当我改变你的版本时,我做了,否则我只是pullinh字符串,让$ watch做实际的绑定。 –