AngularJS,更改动画:未调用指令$watch
AngularJS, Animate on change: directive $watch not called
我正在创建一个实时的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
上的第一个参数,使其成为返回变量的函数,而不是变量本身:
scope.$watch(function () {
return scope.animateWatch; },
function (nv, ov) {
call(); },
true);
请看一下手表部分的文档。
相关文章:
- 显示指令时调用指令方法
- AngularJS:如何调用指令中定义的函数'控制器的作用域
- 如何在ngRepeat中调用指令
- 未对输入更改调用指令
- 从子指令调用指令函数不起作用
- 从 AngularJS 中的父控制器调用指令的方法
- 从服务 AngularJS + TypeScript 调用指令
- 如何从 Angular 上的控制器调用指令的函数
- 如何从 JavaScript 调用指令函数
- Angular JS 如何从指令模板内容事件调用指令范围函数
- 从控制器调用指令中函数的最佳方法是什么
- 从传入的内容调用指令方法
- Angularjs-在ng-init函数完成后调用指令
- 从另一个指令调用指令
- 从父指令角度调用指令函数
- 如何从控制器调用指令作用域上的函数
- 有棱角的从html调用指令中的函数
- 动态元素未在AngularJS上调用指令方法
- 是'不良做法'用于Angular控制器调用指令上的函数
- AngularJS:从具有隔离作用域的控制器中调用指令中的函数