简体   繁体   English

在ng-repeat上的模型之间过渡

[英]Transitioning between models on a ng-repeat

I'm building a directive (Angular 1.2) that will toggle between displaying two different lists - think of them as a list of "trending" items on the site and a list of items the user is "following". 我正在构建一个指令(Angular 1.2),该指令将在显示两个不同的列表之间进行切换-将它们视为站点上“趋势”项的列表和用户正在“跟踪”的项的列表。 So we have a toggle that allows the user to choose which list is being displayed, with an ng-repeat below it showing the items. 因此,我们有一个切换开关,允许用户选择要显示的列表,并在其下方显示一个ng-repeat项。

There will be a good deal of overlap between these lists, and when the user toggles from one list to the other, I'd like items that are contained in both to transition from their places on the "outgoing" list to their places on the new one, rather than disappearing and reappearing. 这些列表之间会有很多重叠,并且当用户从一个列表切换到另一个列表时,我希望两个列表中包含的项目都从“待发”列表上的位置过渡到他们在列表上的位置。新的,而不是消失并重新出现。

My question isn't about how to achieve the actual animations (we're using ngAnimate ), but about how I should structure the controller/data to. 我的问题不是关于如何实现实际的动画(我们正在使用ngAnimate ),而是关于我应该如何构造控制器/数据。 I'm thinking about my directive controller having a trendingList and a followingList (that contain the actual data items), and an activeList that points to whichever of the two are currently being displayed. 我正想着有我的指令控制器trendingListfollowingList (包含实际数据项),以及activeList指向当前显示取其两者。 So the ng-repeat is actually on activeList , and toggling is essentially: 因此,ng-repeat实际上在activeList ,并且切换本质上是:

$scope.toggleMode = function(){
    if ($scope.mode == 'trending') 
        $scope.mode = following; 
        $scope.activeList = followingList;
    else {/*the inverse...*/}
}

Is that the most reasonable approach? 那是最合理的方法吗? If so, how do I ensure that angular recognizes the equality of objects present in both lists? 如果是这样,如何确保角度识别两个列表中存在的对象相等?

Or is there an easier/cleaner way to do this? 还是有一种更容易/更清洁的方法来做到这一点?

From what I understand, angular isn't tracking the equality of objects in the list it is repeating. 据我了解,角度并没有跟踪它重复的列表中对象的相等性。 The animations are triggered when the DOM is manipulated, not when the data changes (though data changes will change the DOM). 动画是在操作DOM时触发的,而不是在数据更改时触发的(尽管数据更改会更改DOM)。 As to what you should do, I don't have experience with this problem so hopefully somebody can point you in the right direction! 至于您应该怎么做,我没有这个问题的经验,所以希望有人可以为您指明正确的方向!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM