WordPress RESTful API V2 + AngularJs +单个帖子分页+自定义帖子类型

时间:2017-01-24 15:13:18

标签: angularjs wordpress rest api

有没有办法使用WP RESTful V2获取Next / Prev路由,而我们在一个帖子页面上?

portfolio-single.html(模板)

<div class="container">
    <h1 class="title">{{title}}</h1>
</div>
<a href="#/portfolio/{{prevPortfolio}}">Prev</a>
<a href="#/portfolio/{{nextPortfolio}}">Next</a>

控制器

angular.module('app')
  .controller('PortfolioCtrl', function ($scope, $routeParams, wpFactory) {

    wpFactory.getPortfolioSingle($routeParams.id)
        .then(function (succ){
            ignoreLoadingBar: false;
            $scope.title = succ.title.rendered;
            $scope.img = succ.better_featured_image.media_details.sizes.large.source_url;

            $scope.currentItemData = succ;
            $scope.currentId = $routeParams.id;


            if($routeParams.id > 0){
                $scope.prevPortfolio = ($routeParams.id) - 1;
            }
            else{
                $scope.prevPortfolio = $scope.currentItemData.length-1;
            }

            if($routeParams.id < $scope.currentItemData.length-1){
                $scope.nextPortfolio = ($routeParams.id)+1;
            }
            else{
                $scope.nextPortfolio = 0;
            }

        }, function error(err) {
            console.log('Errror: ', err);
        });
  });

注意: 我只使用WordPress作为后端,并使用Angular开发完整的单独前端。

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码访问路线更改事件:

$rootScope.$on("$routeChangeStart", function(event, next, current) {
    console.log(current); // Current route information
    console.log(next);    // Future route information
});

有关详细信息,请参阅$route documentation

JSFiddle demo