[英]AngularJs: How to start animation on value change (ng-scope)
Let's say I have a template: 假设我有一个模板:
<div ng-controller="ValueCtrl">
<dl>
<dt>Points</dt>
<dd>{{ points }}</dd>
</dl>
</div>
and a controller: 和一个控制器:
app.controller('ValueCtrl', function($scope) {
$scope.points = 100;
$scope.$on('points:changed', function(newPoints) {
$scope.points = newPoints;
});
});
In my app I use animate.css from http://daneden.github.io/animate.css/ . 在我的应用程序中,我使用http://daneden.github.io/animate.css/中的 animate.css。
I would like to have points
value change animated. 我想让
points
值更改动画化。 While I could add animated pulse
CSS classes to the element via Javascript, I don't want to manipulate the DOM directly from the controller. 虽然我可以通过Javascript向元素添加
animated pulse
CSS类,但我不想直接从控制器操作DOM。
Is there a simple way I can detect a change through CSS? 有没有一种简单的方法可以通过CSS检测更改? When using
ng-repeat
or ng-show
etc. I can detect those by using CSS classes (like ng-enter
, ng-leave
etc). 当使用
ng-repeat
或ng-show
等时,我可以通过使用CSS类(例如ng-enter
, ng-leave
等)来检测它们。 Is there a way to detect similar events for elements that are bound to the scope? 有没有一种方法可以检测到绑定到作用域的元素的类似事件?
The way to go is to use ng-class
to make the class conditional on a scope variable (I'll use $scope.changed
, a boolean). $scope.changed
的方法是使用ng-class
使该类取决于范围变量(我将使用$scope.changed
,一个布尔值)。
HTML HTML
<dl>
<dt>Points</dt>
<dd ng-class="{'animated bounce' : changed}">{{ points }}</dd>
</dl>
Since the animate.css
lib cannot reset the $scope.changed
signal variable, we have to do this after a certain time, so subsequent changes are able to trigger an animation. 由于
animate.css
库无法重置$scope.changed
信号变量,因此我们必须在一定时间后执行此操作,因此后续更改可以触发动画。
Javascript Java脚本
$scope.$on('points:changed', function(event, newPoints) {
$scope.points = newPoints.points;
$scope.changed = true;
$timeout(function() {
$scope.changed = false;
}, 1000);
});
I have created a complete example as a JSBin here . 我在这里创建了一个完整的示例作为JSBin 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.