我有一个需要访问url参数的控制器。但是,我无法弄清楚如何访问参数。以下是我到目前为止的情况:
控制器:
function CustomerCtrl($scope, $http, $routeParams, $route) {
// var customer_id = $routeParams.id; // this doesn't work
// var customer_id = $route.current.params.id; // this doesn't work
var customer_id = '58'; // this clearly works
$http({
url: 'customers/'+customer_id+'/info',
method: "POST"
})
.success(function (data, status, headers, config) { $scope.name = data; })
.error(function (data, status, headers, config) { $scope.status = status; });
}
应用:
var customer = {
name: 'customer',
url: '/customer',
abstract: true,
templateUrl: 'views/customer/customer.html',
controller: 'CustomerCtrl'
};
var customer_info = {
name: 'customer.info',
parent: customer,
url: '/:id/info'
views: {
view1: { templateUrl: "views/customer/view1.html" },
view2: { templateUrl: "views/customer/view2.html" }
}
};
$stateProvider
.state(customer)
.state(customer_info);
我错过了什么?
答案 0 :(得分:1)
实际上比你想象的还要容易。您使用的是使用状态而不是默认路由器的ui-router(https://github.com/angular-ui/ui-router)。
function CustomerCtrl($scope, $http, $routeParams, $route) {
应该是:
function CustomerCtrl($scope, $http, $stateParams, $state) {
然后:
var customer_id = $stateParams.id;
答案 1 :(得分:0)
我花了一整天的时间来试图找到一个看似简单,常见问题的有条理的解决方案。大多数人可能已经想到了这一点,但解决方案是使用普通的旧javascript:
var pathArray = window.location.pathname.split( '/' );
var customer_id = pathArray[3];
$scope.customer_id = customer_id
如此简单,但我觉得从一开始就没想过这个就是白痴。