How to set state params in Angularjs?

In AngularJS, using the UI-Router library is a popular choice for managing application states and handling state parameters. Here’s a basic example of setting state parameters using AngularJS and UI-Router:

  1. Include AngularJS and UI-Router libraries in your project:


<script src=""></script>
<script src=""></script>
  1. Create an AngularJS module and configure states:

Define your AngularJS module and configure the states using $stateProvider:


angular.module('app', ['ui.router'])
  .config(function($stateProvider, $urlRouterProvider) {
      .state('home', {
        url: '/home',
        templateUrl: 'home.html',
        controller: 'HomeController'
      .state('details', {
        url: '/details/:id',
        templateUrl: 'details.html',
        controller: 'DetailsController'


In this example, we define two states, home and details. The details state has a URL parameter called id specified using the syntax :id.

  1. Create controllers for your states:


.controller('HomeController', function($scope, $state) {
  $scope.goToDetails = function(id) {
    $state.go('details', { id: id });
.controller('DetailsController', function($scope, $stateParams) {
  $ = $;

In the HomeController, we define a function goToDetails that navigates to the details state and sets the id parameter. In the DetailsController, we read the id parameter from $stateParams.

  1. Create the corresponding HTML templates:


  <button ng-click="goToDetails(42)">Go to Details with ID 42</button>


  <p>ID: {{id}}</p>
  1. Set up the main HTML file:
<!DOCTYPE html>
<html ng-app="app">
    <script src=""></script>
    <script src=""></script>
    <script src="app.js"></script>
    <div ui-view></div>

    <script type="text/ng-template" id="home.html">
      <!-- Include home.html content here -->

    <script type="text/ng-template" id="details.html">
      <!-- Include details.html content here -->

In this example, when you click the “Go to Details with ID 42” button on the home page, the application will navigate to the details state with the id parameter set to 42. The DetailsController reads the id parameter and displays it on the details page.

This is a simple example of setting state parameters using AngularJS and the UI-Router library. You can expand on this to manage more complex application states and pass multiple parameters between states.


Table of Contents

Related posts