簡體   English   中英

在角離子中使用狀態傳遞參數

[英]passing parameters using state in angular 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)
});

'OrderCtrl'屬於狀態

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

而參數orderid是在子狀態下定義的:

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

這意味着,父級無法訪問這些參數-因為它們是為其子級定義的

因此,我們可以1)將參數移至父級(我想這就是方法)

一個正在工作的矮人

  .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;
}])

這些示例狀態有一個工作正常的人

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM