How to pass parameters using ui-sref in ui-router to controller

358    Asked by AashnaSaito in Java , Asked on Jun 22, 2021

 I need to pass and receive two parameters to the state I want to transit to using ui-sref of ui-router.

Something like using the link below for transitioning the state to home with foo and bar parameters:
Go to home state with foo and bar parameters

Receiving foo and bar values in a controller:

app.controller('SomeController', function($scope, $stateParam) {
  //..
  var foo = $stateParam.foo; //getting fooVal
  var bar = $stateParam.bar; //getting barVal
  //..
});     

I get undefined for $stateParam in the controller.

Could somebody help me understand how to get it done?

Edit:

.state('home', {
  url: '/',
  views: {
    '': {
      templateUrl: 'home.html',
      controller: 'MainRootCtrl'
    },
    'A@home': {
      templateUrl: 'a.html',
      controller: 'MainCtrl'
    },
    'B@home': {
      templateUrl: 'b.html',
      controller: 'SomeController'
    }
  }
});

Answered by Abdul Rehman

Your Answer

Interviews

Parent Categories