Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass parameter when I redirect the page in angularjs using ui router?

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.

like image 955
FlyingCat Avatar asked Aug 14 '14 01:08

FlyingCat


People also ask

How to redirect to another page in AngularJS with parameters?

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.

What is UI sref in AngularJS?

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.

What is UI Router in AngularJS?

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.


3 Answers

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;
})
like image 161
Darshan P Avatar answered Oct 22 '22 11:10

Darshan P


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'
    })
like image 37
PSL Avatar answered Oct 22 '22 12:10

PSL


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.

like image 1
Ethan Kim Avatar answered Oct 22 '22 10:10

Ethan Kim