简体   繁体   English

控制器中ui路由器的参数和解析

[英]A parameter of ui-router in controller and resolve

I have configured the following ui-router. 我已经配置了以下ui路由器。

app.config(['$stateProvider', function ($stateProvider) {
    $stateProvider
        .state('global.editor', {
            url: '/posts/editor/{id}',
            templateUrl: '/htmls/editor.html',
            controller: 'EditorCtrl',
            resolve: {
                post: ['$stateParams', 'codeService', function ($stateParams, codeService) {
                    return codeService.getPost($stateParams.id)
                }]
            }
        }
        .state('global.new', {
            url: '/new',
            templateUrl: '/htmls/editor.html',
            controller: 'EditorCtrl'
        })
        .state('global.newTRUE', {
            url: '/newTRUE',
            templateUrl: '/htmls/editor.html',
            controller: 'EditorCtrl'
        })           
        .state('global.editor.panels', {
            controller: 'PanelsCtrl',
            params: { layout: 'horizontal' },
            templateUrl: function (params) { return "/htmls/" + params.layout + '.html' }
        }
}])

app.controller('EditorCtrl', ['$scope', '$state', function ($scope, $state) {
    $scope.layout = "horizontal";
    $scope.$watch('layout', function () {
        $state.go('global.editor.panels', { layout: $scope.layout });
    });
}]);

As a result, https://localhost:3000/#/new in a browser leads to (the state global.editor , then to) the state global.editor.panels . 结果,浏览器中的https:// localhost:3000 /#/ new导致(状态为global.editor ,然后是)状态global.editor.panels

Now, I want to add a parameter connected : 现在,我想添加一个connected参数:

  1. I don't want it to be shown in the url 我不希望它显示在网址中
  2. https://localhost:3000/#/new in a browser makes connected to be false , and https://localhost:3000/#/newTRUE in a browser makes connected to be true 浏览器中的https:// localhost:3000 /#/ new使得connectedfalse ,浏览器中的https:// localhost:3000 /#/ newTRUE使得connectedtrue
  3. connected can be past into the controller EditorCtrl and PanelsCtrl 可以将已connected的控制器EditorCtrl到控制器的EditorCtrlPanelsCtrl
  4. connected can be available in the resolve of global.editor ; connected可以在可用的resolveglobal.editor ; ideally, we could resolve different objects according to the value of connected . 理想情况下,我们可以根据的值来解决不同的对象connected

Does anyone know how to accomplish this? 有谁知道如何做到这一点?

You can add resolve for new and newTRUE: 您可以为new和newTRUE添加解析:

 .state('global.new', {
     url: '/new',
     templateUrl: '/htmls/editor.html',
     resolve: {
         isConnected: function() {
             return false;
         }
     },
     controller: 'EditorCtrl'
 })
 .state('global.newTRUE', {
     url: '/newTRUE',
     templateUrl: '/htmls/editor.html',
     resolve: {
         isConnected: function() {
             return true;
         }
     },
     controller: 'EditorCtrl'
 })

And in EditorCtrl (or PanelsCtrl) you can use it like: 在EditorCtrl(或PanelsCtrl)中,您可以像这样使用它:

app.controller('EditorCtrl', ['$scope', '$state', 'isConnected', function($scope, $state, isConnected) {
    console.log("connected : " + isConnected); // you can save this value in Service and use it later.
    ...
}]);

You can use classic approach - in resolve 您可以使用经典方法-解决

Or you can use hidden parameters from angular ui router. 或者,您可以使用来自角度ui路由器的隐藏参数。 Define params : {isConnected' : null} in your parent global state. 在您的父global状态下定义params : {isConnected' : null} In: 在:

  • global.newTRUE - set value in $state config global.newTRUE-在$ state配置中设置值
  • global.new - set value in $state config global.new-在$ state配置中设置值
  • global.editor.panels - set parameters in transition/go or ui-sref global.editor.panels-在transition/goui-sref设置参数

definition is like this: 定义是这样的:

$stateProvider
      .state('global.newTRUE', {
        url : '/:newTRUE',
        params : {
          'isConnected' : false
        }
   });
}

and in controller you get in from $stateParams . 在控制器中,您可以从$stateParams获得。 Problem with this approach is hidden parameters are loses in refresh page, except if is set default value 这种方法的问题是刷新页面中的隐藏参数丢失,除非设置了默认值

You can surely use the params of UI-Router states' config to not show it in URL and achieve all mentioned points. 你肯定可以使用params UI-路由器状态的配置,以在URL没有表现出来,并实现所有提到的观点。

Also, as per #2 , you need connected to be false for /new and true for /newTRUE . 此外,按照#2的要求 ,对于/newconnected必须为false ;对于/newTRUEconnected必须为true We can do so by passing true or false as default value for those states. 我们可以通过传递truefalse作为这些状态的默认值来实现。 Something like this: 像这样:

$stateProvider
    .state('global.editor', {
        url: '/posts/editor/{id}',
        templateUrl: '/htmls/editor.html',
        params: { connected: null },
        controller: 'EditorCtrl',
        resolve: {
            post: ['$stateParams', 'codeService', function ($stateParams, codeService) {
                return codeService.getPost($stateParams.id)
            }]
        }
    }
    .state('global.new', {
        url: '/new',
        templateUrl: '/htmls/editor.html',
        params: { connected: false }, // default false for /new
        controller: 'EditorCtrl'
    })
    .state('global.newTRUE', {
        url: '/newTRUE',
        templateUrl: '/htmls/editor.html',
        params: { connected: true }, // default true for /newTRUE
        controller: 'EditorCtrl'
    })           
    .state('global.editor.panels', {
        controller: 'PanelsCtrl',
        params: { layout: 'horizontal', connected: null },
        templateUrl: function (params) { return "/htmls/" + params.layout + '.html' }
    }

For #3 , In order to access connected in your controllers ( EditorCtrl and PanelsCtrl ) you can inject $stateParams to controller and use $stateParams.connected to get it. 对于#3,为了访问connected在你的控制器( EditorCtrlPanelsCtrl )你可以注入$stateParams到控制器,并使用$stateParams.connected得到它。

For #4 , ( This is more or less similar to achieveing #3 ) 对于#4 ,( 或多或少与实现#3相似

Just like you get $stateParams.id , you can have $stateParams.connected as well, which you can use to resolve different objects according to the value of connected . 就像你得到$stateParams.id ,你可以有$stateParams.connected还有,你可以用它根据价值来解决不同的对象connected Something like this: 像这样:

.state('global.editor', {
    url: '/posts/editor/{id}',
    templateUrl: '/htmls/editor.html',
    params: { connected: null },
    controller: 'EditorCtrl',
    resolve: {
        post: ['$stateParams', 'codeService', function ($stateParams, codeService) {
            return $stateParams.connected ? 
                codeService.getPost($stateParams.id) :
                codeService.getSomethingElse($stateParams.id) 
        }]
    }
}

But, for that to work, make sure that you are passing connected as params when you visit global.editor state (using $state.go or ui-sref ) 但是,要使其正常工作,请确保在访问global.editor state(使用$state.goui-sref )时, global.editor connected传递为参数。

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM