简体   繁体   English

在AngularJS中获得“下一个”和“上一个”项目?

[英]Get “next” and “previous” items in AngularJS?

I'm using ngRoute to filter through a list of stories into one specific story when clicked, as below: 我使用ngRoute在单击时将故事列表筛选为一个特定故事,如下所示:

myApp.config(['$routeProvider', function($routeProvider) {
    $routeProvider.
    when('/stories', {
        templateUrl: '/partials/stories.html',
        controller: 'StoryListCtrl'
    }).
    when('/stories/:storyID', {
        templateUrl: '/partials/story.html',
        controller: 'StoryDetailCtrl'
    });
}]);

This works fine, but how can I get the "next" and "previous" stories. 效果很好,但是如何获得“下一个”和“上一个”故事。 The user needs to be able to swipe left and right between the stories, but I can't simply load them all - I need to load each story and one either side of it. 用户需要能够在故事之间左右滑动,但是我不能简单地加载所有故事-我需要加载每个故事以及故事的任一侧。 Then, when the user swipes either way, one story is removed and a new one is added. 然后,当用户以任意一种方式滑动时,一个故事将被删除,而新故事将被添加。

Is this possible with Angular? Angular有可能吗? I've searched like crazy but can't find any reference to this online anyway. 我已经疯狂地搜索过,但无论如何都找不到在线参考。

Thanks for your time! 谢谢你的时间!

you can use angular ng-swipe for this. 您可以为此使用角度ng-swipe。 eg Let's say, you have these files as follows. 例如,假设您具有以下文件。

story.html story.html

<div ng-swipe-left="nextStory()" ng-swipe-right="previousStory()" >
    {{storyID}}
</div>

StoryDetailCtrl StoryDe​​tailCtrl

myApp.controller('StoryDetailCtrl', function ($scope, $routeParams, $location) {
    $scope.storyID = $routeParams.storyID;
    $scope.nextStory = function () {
            var storyID = $routeParams.storyID;
            var path = 'stories/' + (storyID + 1 );
            $location.path(path);
            //console.log("This is left Swipe.");
        };

        $scope.previousStory = function () {
            var storyID = $routeParams.storyID;            
            var path = 'stories/' + (storyID - 1 );}            
            $location.path(path);
            //console.log("This is right Swipe.");
        };
});

Make sure to inject ngTouch as dependency in your module as ['ngTouch'] 确保在模块中将ngTouch作为依赖项注入为['ngTouch']

Hopefully, it makes sense to you. 希望这对您有意义。

When the user swipes you need to send a query with the current id and the orientation, so in that way you'll know is right is next and left if previous, being said that you can query 当用户滑动时,您需要发送带有当前ID和方向的查询,这样,您将知道“正确的是下一个”和“左边的”(如果是以前的话),也就是说您可以查询

Right: take 1 greater than your current id 正确:比您当前的ID大1

Left: take 1 less than your current id 左:比您当前的ID小1

No orientation: just load the current id 无方向:仅加载当前ID

for Swiping you could use the $swipe service, ngSwipeLeft and ngSwipeRight as well. 对于刷卡,您还可以使用$ swipe服务,ngSwipeLeft和ngSwipeRight。

you could resolve a function prior to the route /stories/:storyID execution, by passing the id, and orientation, or you can modify your route to support orientation like /stories/:storyID/:orientation and orientation can be null 您可以在传递/ stories /:storyID之前通过传递ID和方向来解析功能,也可以修改路由以支持方向,例如/ stories /:storyID /:orientation,并且方向可以为null

take a look to this article that you may have as an example, I just would like to highlight the how to calculate the current page and the swipe functionality in angularjs, that may apply to your scenario. 看看这个文章 ,你可能有作为一个例子,我只是想强调的是如何计算的当前页和angularjs的刷卡功能,可能适用于您的方案。

angular.module('website', ['ngAnimate', 'ngTouch'])
    .controller('MainCtrl', function ($scope) {
        /* Code omitted */

        $scope.direction = 'left';
        $scope.currentIndex = 0;

        $scope.setCurrentSlideIndex = function (index) {
            $scope.direction = (index > $scope.currentIndex) ? 'left' : 'right';
            $scope.currentIndex = index;
        };

        $scope.isCurrentSlideIndex = function (index) {
            return $scope.currentIndex === index;
        };

        $scope.prevSlide = function () {
            $scope.direction = 'left';
            $scope.currentIndex = ($scope.currentIndex < $scope.slides.length - 1) ? ++$scope.currentIndex : 0;
        };

        $scope.nextSlide = function () {
            $scope.direction = 'right';
            $scope.currentIndex = ($scope.currentIndex > 0) ? --$scope.currentIndex : $scope.slides.length - 1;
        };
    })

cheers! 干杯!

Let's say you have a list of stories in which you show in /stories page, now using <a ng-href="yourCtrl.getStoryHref()" />Full Story</a> ; 假设您有一个要在/stories页面中显示的故事列表,现在使用<a ng-href="yourCtrl.getStoryHref()" />Full Story</a> Now you move to story page which shows full story. 现在,您将转到显示完整故事的故事页面。 Here you have prev and next links which will take you to prev/next story <a ng-href="yourCtrl.getNextStoryHref()" />Next Story</a> and <a ng-href="yourCtrl.getPrevStoryHref()" />Previous Story</a> 在这里,您具有上一个和下一个链接,这些链接将带您到上一个/下一个故事<a ng-href="yourCtrl.getNextStoryHref()" />Next Story</a><a ng-href="yourCtrl.getPrevStoryHref()" />Previous Story</a>

Alternately if you want to use it on mobile, you can use ng-touch or fastclick . 或者,如果您想在移动设备上使用它,则可以使用ng-touch或fastclick。 In ng-touch you can use ng-swipe-left and ng-swipe-right 在ng-touch中,您可以使用ng-swipe-leftng-swipe-right

<!-- Your Story Div -->
<div ng-swipe-right="yourCtrl.moveToNextStory">Content</div>

In your moveToNextStory function inside controller, set the $location.path to the path of next story 在控制器内部的moveToNextStory函数中,将$ location.path设置为下一个故事的路径

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

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