本文介绍了角度UI路由器|$stateParams 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎 $stateParams 不起作用.像这样传递日期:

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

参数在目标状态上被忽略

console.log('state2 params:', $stateParams);//返回空对象 {}

代码:

 var app = angular.module('app', ['ui.router']);app.config(function($stateProvider) {$stateProvider.state('state1', {网址:'',templateUrl: 'state-1.html',控制器:函数($scope,$state,$stateParams){$scope.go = 函数 () {$state.go('state2', { someParam : 'broken magic' });};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){控制台日志($stateParams);}}) ...

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

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

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.

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

08-12 15:06