我正在打电话给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请求无法通过。
我不知道我想念什么?
尝试改变$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] 删除。
我来说两句