如何通过参数时重定向我使用的UI路由器angularjs的页面? [英] How to pass parameter when I redirect the page in angularjs using ui router?
本文介绍了如何通过参数时重定向我使用的UI路由器angularjs的页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想通过通过用户界面路由器state.go参数
I am trying to pass parameters via the ui-router state.go
不过,我不知道如何传递的参数。这里是我的codeS
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'
})
});
现在添加在第一个控制器
Now add in first controller
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屋!
查看全文