2014-11-14 42 views
1

我有一個NG重複項模板以下屬性...檢測第一數據綁定

ng-class='$first ? "newItem" : ""' 

當我添加一個新的項目,以我的模型與一個按鈕,點擊這個偉大的工程。但我不希望列表中的第一個項目在頁面首次加載時進行動畫製作。

所以,我怎麼都可以應用的第一個元素newItem類只當它的一個新添加的元素到模型?

+0

你可以只保存一個變量,如果loeaded'wasEmpty = myArray.lenght == 0'那麼你可以'納克級='$ &&首當wasEmpty列表是空的? 「newItem」:「」'' – 2014-11-14 19:09:49

+0

啊......這個列表在加載時不是空的。 – 2014-11-14 19:13:43

+0

也許第二堂課,沒有動畫,是最好的方法。 – Blazemonger 2014-11-14 19:22:44

回答

1

您可以使用模塊ngAnimate
當你加載它沒有初始化,並且只有當你添加一個新的項目。

angular.module('App', ['ngAnimate']) 
 
    .controller('MainController', ['$scope', function($scope) { 
 
     $scope.items = [1, 2, 3]; 
 
     $scope.add = function() { 
 
      $scope.items.push($scope.items.length + 1); 
 
     }; 
 
    }]);
.item.ng-enter { 
 
    -webkit-transition: all 1s linear; 
 
    transition: all 1s linear; 
 
} 
 
.item.ng-enter.ng-enter-active { 
 
    background-color: green; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js"></script> 
 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular-animate.js"></script> 
 
<div ng-app="App" ng-controller="MainController"> 
 
    <button ng-click="add()">Add item</button> 
 
    <div class="item" ng-repeat="item in items">{{item}}</div> 
 
</div>

+0

哇。這是一個很好的答案。 – 2014-11-15 13:32:46