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
1 Answer
Reset to default 3The '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',
})