Pass state parameter from url in angular-ui-router


URL parameters are automatically sent as state parameters in angular-ui-router. All we need to do is declare the parameter names in state configuration. Here is an example of state configuration:


.state('home.profile', {
    url: '/profile?id',
    templateUrl: 'templates/profile.html',
    controller: 'ProfileCtrl'
})


Here we've declared a parameter id in url attribute. 

Now if we hit the url with parameter id e.g /profile?id=1234343, we'll be able to receive id=1234343 in state params. 

We'll also be able to pass state parameter through ui-sref like the following:

<a ui-sref="home.profile({id:'1234343'})">Go to profile</a>

Here is an example controller that demonstrates how to receive the parameter:

.controller('ProfileCtrl', ['$scope', '$stateParams',
    function ($scope, $stateParams) {
        $scope.id = $stateParams.id; // id will be
1234343    
    }
])


Comments

Popular posts from this blog

Run tasks in background in Spring

How to configure Wildfly 10 to use MySQL

Conditional field inclusion in Jackson and Spring Boot