[英]How to send parameters in Angularjs $state.go?
I am developing AngularJS application. 我正在开发AngularJS应用程序。 I am trying to send values in $state.go
to different states. 我试图将$state.go
值发送到不同的州。 I am trying as below. 我正在尝试如下。
This is my state: 这是我的状态:
$stateProvider
.state('Registration.OTPVerification', {
url: '/RegistrationOTPVerification',
templateUrl: 'Registration/RegistrationOTP.html',
controller: 'RegistrationOTPVerification'
});
I am trying as below. 我正在尝试如下。
var customerid = response.data.ID;
var OTP = response.data.OTP;
$state.go('Registration.OTPVerification', customerid,OTP);
I am trying to receive parameters in below controller. 我试图在下面的控制器中接收参数。
(function () {
angular.module('RoslpApp').controller('RegistrationOTPVerification', ['$scope', '$http', '$translatePartialLoader', '$translate', '$state', function ($scope, $http, $translatePartialLoader, $translate, $state, $stateParams) {
var customerid = $stateParams.customerid;
var OTP = $stateParams.OTP;
alert(customerid);
}]);
})();
I am not able to receive parameters. 我无法接收参数。 Any help would be appreciated. 任何帮助,将不胜感激。 Thank you. 谢谢。
You need to have those as params
in order to be able to send them through state
. 您需要将这些作为params
,以便能够通过state
发送它们。 Like this: 像这样:
$stateProvider
.state('Registration.OTPVerification', {
url: '/RegistrationOTPVerification',
params: {
customerid: null,
OTP: null
},
templateUrl: 'Registration/RegistrationOTP.html',
controller: 'RegistrationOTPVerification'
});
Now, you can use $state.go
like following: 现在,您可以使用$state.go
,如下所示:
$state.go('Registration.OTPVerification', {
customerid: 123,
OTP: 2323
});
Finally, you can access them way you are using $stateParams.customerid
and $stateParams.OTP
but make sure you have $stateParams
injected just like you have $state
and $translate
injected. 最后,您可以使用$stateParams.customerid
和$stateParams.OTP
访问它们,但请确保注入$stateParams
,就像注入$state
和$translate
。
Link - https://ui-router.github.io/ng1/docs/0.3.1/index.html#/api/ui.router.state .$state 链接 - https://ui-router.github.io/ng1/docs/0.3.1/index.html#/api/ui.router.state。$ state
Syntax - go(to, params, options) params - A map of the parameters that will be sent to the state, will populate $stateParams 语法 - go(to,params,options)params - 将发送到状态的参数的映射,将填充$ stateParams
$stateProvider.state('Registration.OTPVerification', {
url: '/RegistrationOTPVerification/:data',
templateUrl: 'Registration/RegistrationOTP.html',
controller: 'RegistrationOTPVerification'
});
$state.go('Registration.OTPVerification', response);
(function () {
angular.module('RoslpApp').controller('RegistrationOTPVerification', ['$scope', '$http', '$translatePartialLoader', '$translate', '$state', function ($scope, $http, $translatePartialLoader, $translate, $state, $stateParams) {
var customerid = $stateParams.data.ID;
var OTP = $stateParams.data.OTP;
alert(customerid);
}]);
})();
Just use $state.go
as this: 只需使用$state.go
:
$state.go('Registration.OTPVerification',
{
customerid: response.data.ID,
OTP: response.data.OTP
});
Also you need to define the url parameters in the $stateProvider.state
as: 您还需要在$stateProvider.state
定义url参数:
$stateProvider
.state('Registration.OTPVerification', {
url: '/RegistrationOTPVerification',
params: {
customerid: '',
OTP: ''
},
templateUrl: 'Registration/RegistrationOTP.html',
controller: 'RegistrationOTPVerification'
});
Here's the updated controller to get the params: 这是更新的控制器来获取参数:
(function () {
angular.module('RoslpApp').controller('RegistrationOTPVerification', ['$scope', '$http', '$translatePartialLoader', '$translate', '$state', '$stateParams', function ($scope, $http, $translatePartialLoader, $translate, $state, $stateParams) {
var customerid = $stateParams.customerid;
var OTP = $stateParams.OTP;
alert(customerid);
}]);
})();
Try this.
$stateProvider
.state('Registration.OTPVerification', {
url: '/RegistrationOTPVerification',
params: {
customerid: null,
OTP:null
},
templateUrl: 'Registration/RegistrationOTP.html',
controller: 'RegistrationOTPVerification'
})
when calling pass parameter like this: 在调用pass参数时,如下所示:
$state.go('Registration.OTPVerification',{customerid: "gfdg",OTP:"4545"});
I wanted to add my two cents to this. 我想加上我的两分钱。 There is a gotchya in the above solutions that cause others issues. 上述解决方案中有一个问题,导致其他问题。 I use views
in my states and was placing the params
($stateParams) in the views
and it wasn't working. 我在我的州使用views
,并将params
($ stateParams)放在views
,但它无法正常工作。 The params
must exist at the root level of the state controller. params
必须存在于状态控制器的根级别。 I am not certain if you can pass them listed under the views
...and if you can how you would go about passing/retrieving the values. 我不确定你是否可以传递它们列在views
...如果你可以如何传递/检索值。
Here is my initial state controller definition: 这是我的初始状态控制器定义:
.state('tab.clubs', {
cache: true,
url: '/clubs',
views: {
'tab-clubs': {
templateUrl: 'templates/tab-clubs.html',
controller: 'ClubCtrl',
params: {
'refreshClubs' : 0,
'pushAction' : 0,
'pushSub' : null,
'pushMsg' : null,
'pushCode' : null
}
}
}
})
Then in controller: 然后在控制器中:
var payload = {
'pushAction' : 1,
'pushSub' : "blah",
'pushMsg' : "more blah blah",
'pushCode' : code
}
$state.go('tab.clubs',payload) ;
It would route to the proper controller/page, but no parameters were being passed. 它将路由到正确的控制器/页面,但没有传递任何参数。 Until I changed the state provider to the following: 直到我将状态提供程序更改为以下内容:
.state('tab.clubs', {
cache: true,
url: '/clubs',
params: {
'refreshClubs' : 0,
'pushAction' : 0,
'pushSub' : null,
'pushMsg' : null,
'pushCode' : null
},
views: {
'tab-clubs': {
templateUrl: 'templates/tab-clubs.html',
controller: 'ClubCtrl'
}
}
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.