$ routeParams在angularjs中变得未定义?

贝斯琳达N.

我正在打电话给json文件的http呼叫。在第一个控制器中,我获得了类别,在第二个控制器中,我获得了具有特定ID的类别。

网址已使用categoryid更新,但$ routeParams显示为未定义,无法访问该文件。

这是我的代码:

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

在控制器中,我有以下内容:

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);
      })

    })
  }
]);

类别的视图如下:

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

当我单击类别时,URL变为:http:// localhost:8100 /#/ category / 12,但是$ stateparams.category中~/wp/v2/terms/category/" + $routeParams.category的未定义,因此HTTP请求无法通过。

我不知道我想念什么?

ftshtw

尝试改变$routeParams$stateParams每一个实例。显然ui-router使用的是“状态”,而ngRouter使用“路线”。

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);
      })

    })
  }
]);

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

AngularJS $ routeParams未定义

来自分类Dev

var在Promise中变得未定义

来自分类Dev

在angularjs中路由,$ routeParams.energyId未定义

来自分类Dev

角度routeParams未定义

来自分类Dev

变量在whereExist函数中变得未定义-Laravel

来自分类Dev

为什么变量在$(document).keypress中变得未定义?

来自分类Dev

为什么变量在$ {document).keypress中变得未定义?

来自分类Dev

隔离范围变量在链接函数中变得未定义

来自分类Dev

api 服务在 observable 中变得未定义

来自分类Dev

反应-在render()期间“ this”变得未定义

来自分类Dev

在angularjs中未定义工厂

来自分类Dev

在angularjs中未定义Toastr

来自分类Dev

在AngularJS中对象属性未定义

来自分类Dev

函数返回AngularJS中未定义的

来自分类Dev

URLID在angularjs中未定义

来自分类Dev

超时angularjs中的未定义函数

来自分类Dev

AngularJS“ this”在普通对象中未定义

来自分类Dev

AngularJs中的未定义方法

来自分类Dev

在AngularJS中对象属性未定义

来自分类Dev

AngularJS中未定义的变量

来自分类Dev

$ scope在服务AngularJs中未定义:

来自分类Dev

angularjs中的未定义对象

来自分类Dev

在angularjs中未定义函数参数

来自分类Dev

我试图从选择中获取年份,但在jQuery中变得未定义

来自分类Dev

尝试在Node.js中实现连接池时连接变得未定义

来自分类Dev

尝试在Node.js中实现连接池时连接变得未定义

来自分类Dev

angular2变量在发布请求中变得未定义,同步失败

来自分类Dev

为什么返回值在我的递归函数中变得未定义?

来自分类Dev

async.parallel 中的 async await,回调变得未定义