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..
})
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?
Thanks.
Creating a Default Route in AngularJS The below syntax just simply means to redirect to a different page if any of the existing routes don't match. otherwise ({ redirectTo: 'page' }); Let's use the same example above and add a default route to our $routeProvider service. function($routeProvider){ $routeProvider.
A ui-sref is a directive, and behaves similar to an html href . Instead of referencing a url like an href , it references a state. The ui-sref directive automatically builds a href attribute for you ( <a href=...> </a> ) based on your state's url.
Angular-UI-Router is an AngularJS module used to create routes for AngularJS applications. Routes are an important part of Single-Page-Applications (SPAs) as well as regular applications and Angular-UI-Router provides easy creation and usage of routes in AngularJS.
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 });
}
}]);
In second controller inject $stateParams
//my second.html
app.controller.('secondCtrl',["$scope", "$state", "$stateParams", function($scope, $state, $stateParams){
$scope.id = $stateParams.id;
})
You could do this way in the first controller:-
$state.go("second", {'input' : $scope.userInput});
In the second controller inject $stateParams service.
app.controller('secondCtrl',["$scope", "$stateParams", function($scope, $stateParams){
var data = $stateParams.input;
}]);
and register that in your state:
.state('second', {
url: '/second/:input',
templateUrl: 'second.html'
})
Instead of adding additional param in the url, you could do it on the other way.
.state('second', {
url: '/second',
templateUrl: 'second.html',
params: {input:null}
})
All other changes would be same with what other answers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With