角UI路由器| $ stateParams不工作

问题描述:

好像$ stateParams不工作。
传递日期是这样的:

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

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

PARAMS的目标状态被忽略

params being ignored on 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);
                }
            });
    });

活生生的例子可以在这里找到:
http://jsbin.com/weger/5/edit

感谢您。

您无法通过国家之间的任意参数,需要将它们定义为您的 $ stateProvider $ C $的一部分C>定义。例如。

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);
        }
    }) ...

上面会输出定义的ContactID属性的对象。如果你去 /联系人/ 42 ,您 $ stateParams {的ContactID:42 }

有关详细信息,请参阅UI-路由器URL路径的文档。

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