3

我有一个需要访问 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);

我错过了什么?

4

2 回答 2

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; 
于 2013-07-17T00:35:09.187 回答
0

我整天都在迷惑自己,试图为这个看似简单、常见的问题找到一个有角度的解决方案。你们中的大多数人可能已经想到了这一点,但解决方案是只使用普通的旧 javascript:

var pathArray = window.location.pathname.split( '/' );
var customer_id = pathArray[3];
$scope.customer_id = customer_id

如此简单,但我觉得自己像个白痴,从一开始就没有考虑到这一点。

于 2013-07-17T00:16:20.723 回答