【问题标题】:passing parameters using state in angular ionic在角度离子中使用状态传递参数
【发布时间】:2015-09-09 16:36:04
【问题描述】:

我正在使用 ionic 构建应用程序。

现在我想使用状态通过 url 传递一个变量,但它不起作用。最好的方法是什么?

我的做法:

$stateProvider
    .state('orders', {
        url: "/orders",
        abstract: true,
        templateUrl: "views/side-menu.html",
        cache: false,
        controller: 'OrderCtrl'
    })

    .state('orders.view-order', {
        url: "/view-order?orderid", //<---- THIS ONE
        views: {
            'menuContent': {
                templateUrl: "views/view-order.html"
            }
        },
        cache: false
    })

    .state('orders.open-orders', { 
        url: "/open-orders",
        views: {
            'menuContent': {
                templateUrl: "views/open-orders.html"
            }
        },
        cache: false
    })

我有一个基本控制器“仪表板”,我想通过它发送订单使用

$state.go("orders.view-order", {'orderid': '232323232'});

然后是订单控制器...(只显示一个空对象:(

angular.module(appName).controller('OrderCtrl', 
 function($location, $rootScope, $scope, $ionicPopup,
   $state, $stateParams, $auth, $ionicLoading, 
   $timeout, $interval, newOrderService, jwtHelper){

    console.log($stateParams)
});

【问题讨论】:

    标签: javascript angularjs state angular-ui-router


    【解决方案1】:

    'OrderCtrl'属于状态

    .state('orders', {
        controller: 'OrderCtrl'
        ...
    

    虽然参数 orderid 是在子状态上定义的:

    .state('orders.view-order', {
            url: "/view-order?orderid", // here is param defined
    

    这意味着,父级无法访问这些参数 - 因为它们是为其子级定义的

    所以,我们可以 1)将参数移动到父级(我会说这是方式)

    a working plunker

      .state('parent', {
          url: "/parent/:parentId",
          templateUrl: 'tpl.html',
          controller: 'ParentCtrl',
      })
      .state('parent.child', { 
          url: "/child/:childId",
          template: 'this is a child view',
          controller: 'ChildCtrl',
      })
    

    或 2) 使用这个技巧,当我们将 $stateParams 放入 $rootScope 并可以在任何地方观察其最新值时:

    .run(['$rootScope', '$state', '$stateParams',
      function ($rootScope, $state, $stateParams) {
        $rootScope.$state = $state;
        $rootScope.$stateParams = $stateParams;
    }])
    

    a working plunker 带有这些示例状态

      .state('parent', {
          url: "/parent",
          templateUrl: 'tpl.html',
      })
      .state('parent.child', { 
          url: "/child/:childId",
          template: 'this is a child view',
          controller: 'ChildCtrl',
      })
    

    【讨论】:

    • 我明白了,所以我必须将所有参数附加到父母身上,即使它用于孩子?
    • 我试图在我添加的 plunkers 中向您展示一些解决方案。希望这能给你更多关于如何使用强大的 UI-Router 的想法......
    • @innovation 我检查了您的问题并在stackoverflow.com/a/32487049/1679310 此处添加了工作示例和一些解释,希望对您有所帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-23
    • 1970-01-01
    • 2023-04-08
    • 2015-02-03
    • 2020-02-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多