3

I am making a call an http call to a json file. In the first controller I get the categories , and in the second controller I get the category with a certain ID.

The url gets updated with an categoryid but the $routeParams is shown undefined and cannnot access the file.

Here is my code:

$stateProvider.state('categories', {
  url: '/category',
  templateUrl: 'templates/categories.html',
  controller: 'CategoriesCtrl'
});
$stateProvider.state('postC', {
  url: '/category/:category',
  templateUrl: 'templates/postsc.html',
  controller: 'PostCtrl'
});

In the controller I have the following:

angular.module('myapp')

.controller('CategoriesCtrl', ['$scope', '$http',
  function($scope, $http) {
    $http.get("~/wp/v2/terms/category")
      .then(function(res) {
        $scope.categories = res.data;
      })
  }
])

.controller('PostCtrl', ['$scope', '$http', '$routeParams',
  function($scope, $http, $routeParams) {
    $http.get("~/wp/v2/terms/category/" + $routeParams.category).success(function(res) {

      $scope.current_category_id = $routeParams.category;
      console.log($routeParams.category);
      $scope.pageTitle = 'Posts in ' + res.data.name + ':';
      document.querySelector('title').innerHTML = 'Category: ' + res.data.name + ' | AngularJS Demo Theme';

      $http.get("~/wp/v2/posts/?filter[category_name]=" + res.data.name).success(function(res) {
        $scope.posts = res.data;
        console.log($scope.posts);
      })

    })
  }
]);

The view for the categories is the following:

<div class="list">
  <a ng-repeat="category in categories" href="#/category/{{category.id}}" class="item item-thumbnail-left">
    <h2>{{category.name}}</h2>
  </a>
</div>

And when I click on the category the URL becomes: http://localhost:8100/#/category/12, but the $stateparams.category in ~/wp/v2/terms/category/" + $routeParams.category is undefined therefor the http request cannot go through.

I cannot figure it out what am I missing ?

2 Answers 2

3

Try changing $routeParams with $stateParams in every instance. You're obviously using ui-router which uses "states", while ngRouter uses "routes".

angular.module('myapp')

.controller('CategoriesCtrl', ['$scope', '$http',
  function($scope, $http) {
    $http.get("~/wp/v2/terms/category")
      .then(function(res) {
        $scope.categories = res.data;
      })
  }
])

.controller('PostCtrl', ['$scope', '$http', '$stateParams',
  function($scope, $http, $stateParams) {
    $http.get("~/wp/v2/terms/category/" + $stateParams.category).success(function(res) {

      $scope.current_category_id = $stateParams.category;
      console.log($stateParams.category);
      $scope.pageTitle = 'Posts in ' + res.data.name + ':';
      document.querySelector('title').innerHTML = 'Category: ' + res.data.name + ' | AngularJS Demo Theme';

      $http.get("~/wp/v2/posts/?filter[category_name]=" + res.data.name).success(function(res) {
        $scope.posts = res.data;
        console.log($scope.posts);
      })

    })
  }
]);
Sign up to request clarification or add additional context in comments.

3 Comments

so instead of $stateParams I write $routeParams.. I will try now
And also try console.log($stateParams); before $http.get... to see everything before GET happens, just FYI.
Perfect. Thank you so much
2

You might want to double-check the code near

.controller('PostCtrl', ['$scope','$http','$rootScope', function($scope, $http, $routeParams)

it should be

.controller('PostCtrl', ['$scope','$http','$rootScope', '$routeParams', function($scope, $http, $rootScope, $routeParams)

Read more here on syntax of creating a Controller. You are missing injectors.

5 Comments

Ops yes that was a type. I fixed it but still I have the same prb
Are you able to console.log the $routeParams variable? Besides, which one is undefined: $routeParams.category or $routeParams.categoryId?
$routeParams.category is undefined (ignore the categoryid).I consolelog it but it is undefined
Its incorrect answer..'ui.router' doesn;t give information in $routeParams object..
@PankajParkar - yeah, the code snippest has plenty of coding error. I have just correct one of them. Though it was very important, too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.