最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - passing parameters using state in angular ionic - Stack Overflow

programmeradmin0浏览0评论

I'm building an app using ionic.

Now I want to pass a variable trough a url using state but it does not work. What is the best way to to this?

My approach:

$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
    })

i have a base controller "dashboard" from where i want to send to order using

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

and then the order controller... (shows only a empty object :(

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

    console.log($stateParams)
});

I'm building an app using ionic.

Now I want to pass a variable trough a url using state but it does not work. What is the best way to to this?

My approach:

$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
    })

i have a base controller "dashboard" from where i want to send to order using

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

and then the order controller... (shows only a empty object :(

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

    console.log($stateParams)
});
Share edited Sep 9, 2015 at 16:48 Radim Köhler 124k48 gold badges242 silver badges340 bronze badges asked Sep 9, 2015 at 16:36 RezaReza 8801 gold badge10 silver badges29 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

The 'OrderCtrl' belongs to parent state

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

while the parameter orderid is defined on a child state:

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

And that means, that parent cannot access these parameters - because they are defined for its child

So, we can either 1) move parameter to parent (I would say this is the way)

There is 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',
  })

or 2) use this trick, when we place the $stateParams into $rootScope and can observe its latest value everywhere:

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

There is a working plunker with these example states

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

评论列表(0)

  1. 暂无评论