如何列出特定类别的帖子[ionic& angularjs]

时间:2015-07-04 05:15:04

标签: android angularjs wordpress ionic-framework ionic

我正在使用wordpress json api,ionic和angularjs创建一个混合Android应用程序。 我只是坚持一件事:我无法列出特定类别的帖子。我得到了类别列表但无法传递一些参数。 这是代码

controller.js

.controller('CatsCtrl', function($scope, $http) {

  // You can change this url to experiment with other endpoints
  var categoriesApi = 'http://shayarihunt.com/wp-    json/taxonomies/category/terms?_jsonp=JSON_CALLBACK';

  // This should go in a service so we can reuse it
   $http.jsonp( categoriesApi, {cache:true} ).
    success(function(data, status, headers, config) {
      $scope.categories = data;
     console.log( data );
     }).
    error(function(data, status, headers, config) {
  console.log( 'Post load error.' );
 });

})

.controller('CatsPostsCtrl', function($scope, $http) {

  // You can change this url to experiment with other endpoints
   var postsApi = 'http://shayarihunt.com/wp-json/posts?filter[cat]=' + the parameter has to come here, (I dont know what!!!) + '&_jsonp=JSON_CALLBACK';

  // This should go in a service so we can reuse it
    $http.jsonp( postsApi, {cache:true} ).
  success(function(data, status, headers, config) {
  $scope.posts = data;
  console.log( data );
   }).
    error(function(data, status, headers, config) {
      console.log( 'Post load error.' );
    });

   })

CatsCtrl显示所有类别,但CatsPostsCtrl应显示其不属于的所有类别的帖子。 任何帮助表示赞赏。 谢谢!

1 个答案:

答案 0 :(得分:0)

您正在寻找$stateParams的{​​{1}}。有关详细信息,请查看docs

以下是演示用法。单击第二个图标以显示类别ui-router中的帖子。您也可以在jsfiddle找到相同的代码。

hindi-shayari
angular.module('demoApp', ['ionic'])
.config(function($stateProvider, $urlRouterProvider) {
  $stateProvider
  .state('home', {
      url: '/',
      views: {
          home: {
            templateUrl: 'home.html',
            controller: 'CatsCtrl'
      }
    }
  })
  .state('category', {
      url: '/category/:slug',
      views: {
          category: {
              templateUrl: 'category.html',
              controller: 'CatsPostsCtrl'
          }
      }
  });
    $urlRouterProvider.otherwise('/');
})
.controller('CatsCtrl', function($scope, $http) {

  // You can change this url to experiment with other endpoints
  var categoriesApi = 'http://shayarihunt.com/wp-json/posts?_jsonp=JSON_CALLBACK';

  // This should go in a service so we can reuse it
   $http.jsonp( categoriesApi, {cache:true} ).
    success(function(data, status, headers, config) {
      $scope.posts = data;
     console.log( data );
     }).
    error(function(data, status, headers, config) {
  console.log( 'Post load error.', status, headers);
 });

})

.controller('CatsPostsCtrl', function($scope, $http, $stateParams) {

   console.log($stateParams);
    // You can change this url to experiment with other endpoints
   var postsApi = 'http://shayarihunt.com/wp-json/posts?filter[category_name]=' + $stateParams.slug + '&_jsonp=JSON_CALLBACK';
    $scope.category = $stateParams.slug;
  // This should go in a service so we can reuse it
    $http.jsonp( postsApi, {cache:true} ).
  success(function(data, status, headers, config) {
  $scope.posts = data;
  console.log( data );
   }).
    error(function(data, status, headers, config) {
      console.log( 'Post load error.' );
    });

   })