角度UI路由器|$stateParams 不工作路由器、角度、工作、UI

2023-09-06 08:40:42 作者:你抢不走

似乎 $stateParams 不起作用.像这样通过日期:

$state.go('state2', { someParam : '破魔法' });

参数在目标状态被忽略

console.log('state2 params:', $stateParams);//返回空对象 {}
啤酒冲的主页

代码:

 var app = angular.module('app', ['ui.router']);app.config(函数($stateProvider) {$stateProvider.state('state1', {网址:'',templateUrl: 'state-1.html',控制器:函数($scope,$state,$stateParams){$scope.go = 函数 () {$state.go('state2', { someParam : '破魔法' });};console.log('state1 params:', $stateParams);}}).state('state2', {网址:'state2',templateUrl: 'state-2.html',控制器:函数($scope,$state,$stateParams){$scope.go = 函数 () {$state.go('state1', { someOtherParam : '懒惰的蜥蜴' });};console.log('state2 params:', $stateParams);}});});

可以在这里找到实例

谢谢.

解决方案

您不能在状态之间传递任意参数,您需要将它们定义为 $stateProvider 定义的一部分.例如

$stateProvider.state('contacts.detail', {url: "/contacts/:contactId",templateUrl: 'contacts.detail.html',控制器:函数($stateParams){控制台.log($stateParams);}}) ...

上面将输出一个定义了contactId属性的对象.如果您转到 /contacts/42,您的 $stateParams 将是 {contactId: 42}.

有关详细信息,请参阅 UI-Router URL 路由文档.p>

seems like $stateParams is not working. passing date like this:

$state.go('state2', { someParam : 'broken magic' });

params being ignored on the target state

console.log('state2 params:', $stateParams); // return empty object {}

code:

    var app = angular.module('app', [
     'ui.router'
    ]);

    app.config(function($stateProvider) {
      $stateProvider
            .state('state1', {
                url: '',
                templateUrl: 'state-1.html',
                controller : function ($scope, $state, $stateParams) {
                  $scope.go = function () {
                    $state.go('state2', { someParam : 'broken magic' });
                  };

                  console.log('state1 params:', $stateParams);
                }
            })
            .state('state2', {
                url: 'state2',
                templateUrl: 'state-2.html',
                controller : function ($scope, $state, $stateParams) {
                  $scope.go = function () {
                    $state.go('state1', { someOtherParam : 'lazy lizard' });
                  };

                  console.log('state2 params:', $stateParams);
                }
            });
    });

Live example can be found here

thank you.

解决方案

You can't pass arbitrary parameters between states, you need to have them defined as part of your $stateProvider definition. E.g.

$stateProvider
    .state('contacts.detail', {
        url: "/contacts/:contactId",
        templateUrl: 'contacts.detail.html',
        controller: function ($stateParams) {
            console.log($stateParams);
        }
    }) ...

The above will output an object with the contactId property defined. If you go to /contacts/42, your $stateParams will be {contactId: 42}.

See the documentation for UI-Router URL Routing for more information.