首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在AngularJS中为推送到DOM中的新对象添加动画?

如何在AngularJS中为推送到DOM中的新对象添加动画?
EN

Stack Overflow用户
提问于 2015-05-07 09:52:16
回答 1查看 60关注 0票数 0

我正在尝试将动画添加到控制器正在侦听的服务中推送到数组中的新对象,然后使用ng-repeat将其添加到视图中。如何添加动画而不是只显示新对象?

以下是我的视图、控制器和服务的代码。

查看:

代码语言:javascript
运行
复制
<div>
  <h3>Add a quote:</h3>
  <input type="text" ng-model="newQuote.text" placeholder="Quote...">
  <input type="text" ng-model="newQuote.author" placeholder="Author...">
  <button ng-click="addQuote()">Add</button>
</div>

<h1>Quotes</h1>
<div ng-repeat="thisData in someData | filter:searchThis">
  <div id="{{thisData.id}}" class="quotes">
    <p class="quote">"{{thisData.text}}"<br>-{{thisData.author}}</p>
    <button class="deleteButton" ng-class="{on:state}" ng-click="deleteQuote(thisData.text)">X</button>
  </div>
</div>

控制器:

代码语言:javascript
运行
复制
$scope.getData = function(){
    $scope.someData = dataService.getData()
}

$scope.getData()

$scope.newQuote = {}
$scope.addQuote = function(){
    dataService.addData($scope.newQuote)
    $scope.newQuote = ""
}

服务:

代码语言:javascript
运行
复制
var quotes = [
    { id: 0, text: 'Life isn\'t about getting and having, it\'s about giving and being.', author: 'Kevin Kruse'},
    { id: 1, text: 'Whatever the mind of man can conceive and believe, it can achieve', author: 'Napoleon Hill'},
    { id: 2, text: 'Strive not to be a success, but rather to be of value.', author: 'Albert Einstein'},
    { id: 3, text: 'Two roads diverged in a wood, and I took the one less traveled by, And that has made all the difference.', author: 'Robert Frost'},
    { id: 4, text: 'The most difficult thing is the decision to act, the rest is merely tenacity.', author: 'Amelia Earhart'},
    { id: 5, text: 'Life is what happens to you while you\'re busy making other plans.', author: 'John Lennon'},
    { id: 6, text: 'What even is a jQuery?', author: 'Tyler S. McGinnis'}
  ];

  this.getData = function(){
    return quotes
  }
  this.addData = function(someObj){
    if (someObj.text && someObj.author){
        quotes.unshift(someObj)
    } else {
        return "Error"
    }
  }
EN

回答 1

Stack Overflow用户

发布于 2015-05-07 10:58:02

使用模块ngAnimate,您有两个选择:

1)对类.ng-enter.ng-enter.ng-enter-active使用基于css的动画

2)使用基于js的动画

我用CSS动画here创建了一个示例。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30090608

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档