【问题标题】:Passing multiple parameters to state in the path将多个参数传递给路径中的状态
【发布时间】:2015-11-17 10:01:38
【问题描述】:

我有这个状态定义:

         .state("workPlan.list", {
             url: "MainPage",
             templateUrl: "app/workPlan/templates/workPlanList.tmpl.html",
             controller: "workPlanListController",
             controllerAs: "list",
             resolve: {
                 workPlans: ["workPlanServise", workPlanServiseResolver],
                 inspectionAuthority: ["lookupService", inspectionAuthorityResolver],
                 clients: ["lookupService", ClientsResolver]
             }
         })

当前路径是:

/cities#/MainPage

我需要在 URL 中传递一些数据:

 'Table':'Clients'
 'Key':'Name'
 'Value':'ClientId'

 'Table':'Dates'
 'Key':'Desc'
 'Value':'DateId'

 'Table':'Sites'
 'Key':'SiteName'
 'Value':'SiteId'

我需要在 URL 路径中发送上面的数据:

/cities#/MainPage?{'Table':'Clients','Key':'Name','Value':'ClientId'}{...}{...}

然后在状态定义中我需要在解析函数中使用它。

所以我的问题是是否可以以上述格式在路径中发送数据,以及如何在解析函数中访问它。

【问题讨论】:

    标签: angularjs angular-ui-router


    【解决方案1】:

    当然可以,一切都在文档中:ui-router github wiki

    您将使用参数(双冒号 - 例如“:table”)定义您的状态 URL,并在 resolve 函数中使用 $stateParams 服务:

    $stateProvider
    .state('contacts.detail', {
        url: "/MainPage/:table/:key/:value",
        templateUrl: "app/workPlan/templates/workPlanList.tmpl.html",
        controller: "workPlanListController",
        controllerAs: "list",
        resolve: {
            data: ['$stateParams', function($stateParams) {
                console.log($stateParams.table);
            }]
        }
    })
    

    如果你注入$stateParams服务,参数也将在状态控制器中可用。

    【讨论】:

    【解决方案2】:

    调用你的状态

     $state.go('stateName', {test_one: 'para1',test_two: para2, test_three:'para3'});
    

    或

     window.location.href = 
     this.$state.href('stateName',
     {test_one: 'para1',test_two: para2, test_three:'para3'});
    

    你定义你的状态的routes.js。

     $stateProvider
        .state({
          name: 'stateName',
          url: '/test/?test_one&test_two&test_three',
          views: {
            'content@portal-layout': {
              controller: Controller,
              template: require('./test.html')
            },
          }
        })
    

    最后你的网址看起来像这样

     https://test/?test_one=para1&test_two=para2&test_three=para3 
    

    【讨论】:

      猜你喜欢
      • 2021-12-08
      • 1970-01-01
      • 2014-10-05
      • 1970-01-01
      • 2011-10-12
      • 2021-12-02
      • 2018-12-26
      • 2019-12-20
      • 1970-01-01
      相关资源
      最近更新 更多