javascript - Angularjs ng-repeat动画与数组中的相同元素

标签 javascript angularjs

当范围内的数组发生变化时,我使用 angularjs 向表格添加动画。 这是 HTML 代码:

<div ng-app="myapp" ng-controller="myCtrl"> 
        <table>
            <tr ng-repeat="x in items track by $index" class="fade2">                                   
                <td >
                     {{ x }}
                </td>
            </tr>
        </table>
    <button ng-click="add()">add</button>
    <button ng-click="reset()">reset</button>       
</div>

和javascript代码:

var app = angular.module('myapp', ['ngAnimate']);
app.controller('myCtrl', function($scope) {
    $scope.items = ['hello'];

    $scope.add = function() {
        $scope.items.push('hello')
    }

    $scope.reset = function() {
        $scope.items = ['hello','hello','hello','hello'];
    }
});

添加一些 CSS 后,如果我单击“添加”和“重置”按钮,效果很好,但是,当我单击两次重置时,没有动画。实际上数组已更新,但 $scope.items 数组中的元素没有改变。我如何为这种情况添加动画?

最佳答案

我不明白为什么您希望重置每次都具有动画效果,但是 this可以基本做到这一点

$scope.reset = function() {
    $scope.items = [];
    $timeout(function(){
      $scope.items = ['hello','hello','hello','hello'];
    },200);
}

关于javascript - Angularjs ng-repeat动画与数组中的相同元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33929648/

相关文章:

javascript - 如何在 AngularJS 中访问预定义数组

javascript - 如何在 Canvas 上绘制等距地板?

javascript - 尝试在 javascript 中捕获未定义的检查

javascript - 如何在 json 对象中使用这种类型的键?

javascript - 在浏览器中获取我的视频

jquery - HttpStatusCodeResult.StatusDescription 有多可靠?

AngularJS - 定义模块时,第三个参数是数组时的含义是什么?

javascript - jQuery 选择器::选择

javascript - ng-include 被调用的太频繁了

css - AngularJS:自定义字体图标