我正在使用 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
})
我有一个基本 Controller “仪表板”,我想使用它发送订单
$state.go("orders.view-order", {'orderid': '232323232'});
然后是订单 Controller ...(仅显示一个空对象:(
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;
}])
有a working plunker这些示例状态
.state('parent', {
url: "/parent",
templateUrl: 'tpl.html',
})
.state('parent.child', {
url: "/child/:childId",
template: 'this is a child view',
controller: 'ChildCtrl',
})
关于javascript - 使用 Angular ionic 状态传递参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32484987/