0

In my app, I require to attach multiple url with same state - how do i achieve this?

here is my existing state:

.state('createCase', {
        url: '/createCase',
        templateUrl: null,
        controller: null,
        data: {
          requireLogin: false
        },

also I would like to attache another url as like this:

.state('createCase', {
        url: '/createCase?sn=12345', //(12345 is dynamic )
        templateUrl: null,
        controller: null,
        data: {
          requireLogin: false
        },

here is my current function:

function routeConfig($stateProvider, $locationProvider, $urlRouterProvider) {

    $stateProvider
      .state('login', {
        url: '/',
        templateUrl: function(){ 
          console.log('from 1oauth'); 
          return 'app/login/login.html';
        },
        controller: 'loginCtrl as ctrl',
        data: {
          requireLogin: false
        }
      })
      .state('oauth', {
        url: '/oauth',
        templateUrl: function(){

          console.log('from 2oauth');
          return 'app/oauth/oauth.template.html';

        },
        controller: 'OAuthController as ctrl',
        data: {
          requireLogin: false
        }
      })
      .state('createCase', {
        url: '/createCase',
        templateUrl: null,
        controller: null,
        data: {
          requireLogin: false
        }

        }
      })


    // if (isWeb()){  
      $locationProvider.html5Mode({
        enabled: true,
        requireBase: false
      });
    // }

    // $urlRouterProvider.otherwise('/');
    $urlRouterProvider.otherwise('/');

  }

3 Answers 3

1

You don't need to define two different states for same url. In your controller, you can use $state.params object that has query parameters as properties.

Your route

.state('createCase', {
        url: '/createCase',
        templateUrl: null,
        controller: 'MyCtrl',
        data: {
          requireLogin: false
        },
        params: {
          sn: null
        }

Your controller

function MyCtrl($state) {
    console.log($state.params);
}
Sign up to request clarification or add additional context in comments.

2 Comments

at present I URL is : http://localhost:3000/createCase?sn=12345 But when I console like you, I am getting empty object. -
@user2024080 sorry I'm new to ui-router :-) I just updated the route. Notice I defined the parameters we accept in the route and added a default value for sn parameter.
1

Define in your route:

.state('createCase', {
        url: '/createCase?sn',   // bind your query param to URL
        templateUrl: null,
        controller: null,
        data: {
          requireLogin: false
        },

In your controller, inject $stateParams to dependencies and use it with $stateParams.sn whatever you need.

Example:

$state.go('createCase', {sn:1234});  // move to state with a param

$stateParams.sn   // get a value of ==> 1234

Comments

0

Looks like you want to create a parent state and sub states. If you just want to have access to $routeParams you can can pass them to the controller or leave them out when using same state.

(function () {
    'use strict';

    angular.module('app', ['ui.router'])
        .config(routeConfig);

    routeConfig.$inject = ['$stateProvider'];

    function routeConfig($stateProvider) {
        $stateProvider
            .state('createCase', {
                url: '/createCase',
                template: '<h3>Create Case</h3><div ui-view="detail"></div>'
            })
            .state('createCase.detail', {
                parent: 'createCase',
                url: '/:sn',
                views: {  'detail@createCase': {
                        template: '{{sn}}',
                        controller: function($scope, $stateParams){
                            $scope.sn = $stateParams.sn;
                        }
                    }
                }
            })
    }
}());
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8" />
    <title>AngularJS</title>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <script data-require="[email protected]" src="https://code.angularjs.org/1.5.8/angular.js" data-semver="1.5.8"></script>
    <script  src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.3.1/angular-ui-router.js"></script>
</head>

<body ng-app="app">
    <ul>
        <li><a ui-sref="createCase">createCase</a></li>
        <li><a ui-sref="createCase.detail({sn:'123456'})">createCaseDetail 123456</a></li>
        <li><a ui-sref="createCase.detail({sn:''})">createCaseDetail None</a></li>
    </ul>
    <div ui-view=""></div>
</body>

</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.