2

I am trying to creating web page to perform on edit actions in both in parent(first.edit) and child(second.edit) states. I sending related state parameters through URL, those are captured by using regex patterns.

When I click on ui-sref="home.first.edit({firstId:10})" link the controller receiving state parameter firstId correctly i.e 10.

When I click on ui-sref="home.first.second.edit({firstId:10,secondId:20})" link or refresh the the state the controller receiving in correct value for firstId i.e 10/second/20. and it is moving to parent state(home.first)

This is my js file

angular.module('MyApp', [
  'ui.router'
])
  .config(function($stateProvider, $urlRouterProvider) {

   $urlRouterProvider.otherwise('/home');

Here are the states:

$stateProvider.state('home', {
  url: '',
  views: {
    'home_view': {
      templateUrl: 'index.html',
      controller: 'homeController'
    }
  }
})
.state('home.first', {
  url: "/first",
   params:{
    firstId:null
    },
  views: {
    'home_view': {
      templateUrl: "first.html",
      controller: 'firstCtrl'
    }
  }
})
.state('home.first.edit', {
      url: "/{firstId:[0-9]+}/edit",
      views: {
        'first_view': {
          templateUrl: "/edit_first.html",
          controller: 'firstCtrl'
        }
      }
    })
.state('home.first.second', {
    url: "/second",
    params:{
    secondId:null
    }
    views: {
      'first_view': {
        templateUrl: "/second.html",
        controller: 'secondCtrl'
      }

    }
  })
.state('home.first.second.edit', {
    url: "/{secondId:[0-9]+}/edit",
    views: {
      'second_view': {
        templateUrl: "/views/testplan/projects/edit_second.html",
        controller: 'secondCtrl'
      }
    }
  })

Here is the rest of the code with controller definition

  .controller(
    'homeController',
    function($scope, $state, $stateParams) {
      //  console.log($stateParams.firstId);
    }
  )
  .controller(
    'firstCtrl',
    function($scope, $state, $stateParams) {
      console.log($stateParams.firstId);
    }
  )
  .controller(
    'secondCtrl',
    function($scope, $state, $stateParams) {
      console.log($stateParams.secondId);
    }
  );

1 Answer 1

2

There is a working plunker

Your state definition could be simplified like this:

.state('home.first', {
    url: "/first",
    ...
.state('home.first.edit', {
    url: "/{firstId:[0-9]+}/edit",
    ...
.state('home.first.second', {
    url: "/second",
    ...
.state('home.first.second.edit', {
    url: "/{secondId:[0-9]+}/edit",

But the way you are expecting it to work should be like this:

.state('home.first', {
    url: "/first/{firstId:[0-9]+}",
    ...
.state('home.first.edit', {
    url: "/edit",
    ..
.state('home.first.second', {
    url: "/second/{secondId:[0-9]+}",
    ...
.state('home.first.second.edit', {
    url: "/edit",

These links will start to work then:

<a ui-sref="home.first({firstId:1})">
<a ui-sref="home.first({firstId:22})">
<a ui-sref="home.first.edit({firstId:333})">
<a ui-sref="home.first.edit({firstId:44444})">

<a ui-sref="home.first.second({firstId:4444})">
<a ui-sref="home.first.second.edit({firstId:1,secondId:333})">
<a ui-sref="home.first.second.edit({firstId:22,secondId:55555})">

Check it in action here

Sign up to request clarification or add additional context in comments.

1 Comment

thanks. it working now. i was kept state parameters on parent state.

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.