角UI路由器| $ stateParams不工作 [英] angular UI router | $stateParams not working

查看:223
本文介绍了角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 定义。例如。

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.

这篇关于角UI路由器| $ stateParams不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆