2015-09-09 31 views
2

我正在使用離子建立應用程序。在角度離子中使用狀態傳遞參數

現在我想通過一個變量通過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) 
}); 

回答

3

'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', 
    }) 
+0

我看到所以我必須附加所有的參數,即使它用於孩子的父母? – rZaaaa

+1

我試圖告訴你一些解決方案,我加入了強盜。希望這可以給你更多的想法如何使用強大的UI路由器... –

+0

@innovation我檢查了你的問題,並添加了工作示例和一些解釋這裏http://stackoverflow.com/a/32487049/1679310,希望它可以幫助 –

相關問題