AngularJS$stateParams.id始终未定义

AngularJS$stateParams.id始终未定义,angularjs,Angularjs,我花了好几个小时才弄明白我的代码到底出了什么问题$stateParams.id始终未定义。 这是我的控制器: .controller('ordersCtrl', [ '$scope', '$http', '$stateParams', 'orders', 'meals', function($scope, $http, $stateParams, orders, meals){ console.log($stateParams.id

我花了好几个小时才弄明白我的代码到底出了什么问题$stateParams.id始终未定义。 这是我的控制器:

.controller('ordersCtrl', [
    '$scope',
    '$http',
    '$stateParams',
    'orders',
    'meals',
    function($scope, $http, $stateParams, orders, meals){
        console.log($stateParams.id); 
    ...
 }])
.state('orders/{id}', {
    url: '/orders',
    templateUrl: '/orders.html',
    controller: 'ordersCtrl'
})
<a href="#/orders/{{$index}}">Order</a>
我的州

.controller('ordersCtrl', [
    '$scope',
    '$http',
    '$stateParams',
    'orders',
    'meals',
    function($scope, $http, $stateParams, orders, meals){
        console.log($stateParams.id); 
    ...
 }])
.state('orders/{id}', {
    url: '/orders',
    templateUrl: '/orders.html',
    controller: 'ordersCtrl'
})
<a href="#/orders/{{$index}}">Order</a>
和我的链接:

.controller('ordersCtrl', [
    '$scope',
    '$http',
    '$stateParams',
    'orders',
    'meals',
    function($scope, $http, $stateParams, orders, meals){
        console.log($stateParams.id); 
    ...
 }])
.state('orders/{id}', {
    url: '/orders',
    templateUrl: '/orders.html',
    controller: 'ordersCtrl'
})
<a href="#/orders/{{$index}}">Order</a>


有什么问题吗?

您的州应该是:

.state('orders_view', {
    url: '/orders/:id',
    templateUrl: '/orders.html',
    controller: 'ordersCtrl'
})
state()
的第一个参数只是一个标签。它不关心参数。这是为
url
保留的

此外,链接的正确形式为:

<a href ui-sref="orders_view({id: $index})">Order</a>

我认为参数的语法可能不同。您可以尝试如下更改代码:

.state('orders/:id', {
    url: '/orders',
    templateUrl: '/orders.html',
    controller: 'ordersCtrl'
})

我希望这能帮助您

使用索引作为标识符不是很实用。在数组中除结尾以外的任何位置删除或添加一项,书签被销毁。因此,当我只有一个状态时,我该怎么办?请使用永不更改的唯一ID。这不起作用,因为您试图在状态名称上设置$stateParam。