当我在angularjs中使用ui router重定向页面时,如何传递参数
How to pass parameter when I redirect the page in angularjs using ui router?
我正在尝试通过ui-router状态传递参数。go
然而,我不确定如何传递参数。以下是我的代码
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的参数。有人能帮我一下吗?
谢谢。
首先你必须在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
//my second.html
app.controller.('secondCtrl',["$scope", "$state", "$stateParams", function($scope, $state, $stateParams){
$scope.id = $stateParams.id;
})
您可以在第一个控制器中这样做:-
$state.go("second", {'input' : $scope.userInput});
在第二个控制器中注入$stateParams服务
app.controller('secondCtrl',["$scope", "$stateParams", function($scope, $stateParams){
var data = $stateParams.input;
}]);
并在你的州注册:
.state('second', {
url: '/second/:input',
templateUrl: 'second.html'
})
除了在url中添加额外的参数外,还可以采用其他方式。
.state('second', {
url: '/second',
templateUrl: 'second.html',
params: {input:null}
})
所有其他的变化将与其他答案相同。
相关文章:
- 在Flickr API的标签参数中传递一个变量
- 如何知道函数的参数中传递的var名称
- javascript将函数中一个变量的参数值传递给父函数中的另一个
- Jquery-将参数动态传递给函数
- 通过参数将传递的函数分配给动态创建的输入类型的onclick事件
- 使用jQuery deffered.resolveWith()在单个参数中传递数组
- Javascript 函数在与参数一起传递时无法调用
- 将参数值传递给按钮
- 将数组作为参数列表传递给具有现有参数的函数
- 单击窗口.位置,参数已传递
- 在一个参数中传递两个值的 URL 字符串
- 为什么这些参数没有传递给承诺
- 子类化 JavaScript 构造函数参数不传递给数组
- 在流星订阅参数中传递给服务器的内容
- 在url查询参数中传递多个url
- RequireJS:配置参数未传递到包中
- 如何在@Html.ActionLink中的Querystring参数中传递String
- 在一个参数中传递多个参数的对象
- 如何将按钮对象作为函数参数进行传递
- JavaScript在参数中传递新数组