使用ui路由器在angularjs中重定向页面时如何传递参数? [英] How to pass parameter when I redirect the page in angularjs using ui router?
本文介绍了使用ui路由器在angularjs中重定向页面时如何传递参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试通过 ui-router state.go 传递参数
I am trying to pass parameters via the ui-router state.go
但是,我不确定如何传递参数.这是我的代码
However, I am not sure how to pass the parameters. Here are my codes
app.config(function($stateProvider) {
$stateProvider
.state('first', {
url: '/first',
templateUrl: 'first.html'
})
.state('second', {
url: '/second',
templateUrl: 'second.html'
})
})
//my first.html
app.controller.('firstCtrl' ,["$scope", "$state", function($scope, $state){
$scope.userInput <- come from user
$scope.clickThis=function() {
$state.go("second", $scope.userInput);
}
}]);
//my second.html
app.controller.('secondCtrl,["$scope", "$state", function($scope, $state){
//How do I get the parameter that is passed to here..
})
我可以将页面重定向到 second.html,但我似乎无法获取传递给我的 secondCtrl 的参数.有人可以帮我吗?
I can redirect the page to second.html but I can't seem to get the parameter that is passed to my secondCtrl. Can anyone help me about it?
谢谢.
推荐答案
首先要在路由中添加参数.
First you have to add parameter in route.
app.config(function($stateProvider) {
$stateProvider
.state('first', {
url: '/first',
templateUrl: 'first.html'
})
.state('second', {
url: '/second/:id',
templateUrl: 'second.html'
})
});
现在添加第一个控制器
app.controller.('firstCtrl' ,["$scope", "$state", function($scope, $state){
$scope.userInput <- come from user
$scope.clickThis=function() {
$state.go("second", { id: $scope.userInput });
}
}]);
在第二个控制器中注入 $stateParams
In second controller inject $stateParams
//my second.html
app.controller.('secondCtrl',["$scope", "$state", "$stateParams", function($scope, $state, $stateParams){
$scope.id = $stateParams.id;
})
这篇关于使用ui路由器在angularjs中重定向页面时如何传递参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文