Search code examples
javascriptangularjsroutesangular-ui-routerangularjs-routing

$stateParams empty ui router


I have a state as such:

.state('home.deletemsg', {
  views: {
  "contentworker@": {
    url: '/delete/:entityname/:id/:redirectstate',
    templateUrl: "Scripts/proteanapp/templates/delete.html",
    controller: 'deletectrl',
    controllerAs: 'del',
    authenticate: true
   }
}

Then in the controller I have:

return app.controller('deletectrl', ['$scope', '$rootScope', '$stateParams',  function ($scope, $rootScope, $stateParams) {
        debugger;
        // check for ui router error
        var del = this;
        del.entityname = $stateParams.entityname;
        del.entityid = $stateParams.id;
    }]);

Calling $state.go from a controller like :

$state.go('home.deletemsg', { 'entityname': cd.Customer.Name, 'id': cd.Customer.CustomerID }, { 'location': false, 'notify': true });

But the $stateParams is empty, I don't understand why it is empty. I have tried putting an params object into the state and also resolve.

$stateParams.entityname //undefined

$stateParams.id //undefined

Solution

  • url option should be present there on state definition directly, not inside views object of state. But even your controller should not have been called the way you have configured your state.

    Code

    .state('home.deletemsg', {
      //url should present here, rather than putting it inside `views`
      url: '/delete/:entityname/:id/:redirectstate',
      views: {
      "contentworker@": {
        templateUrl: "Scripts/proteanapp/templates/delete.html",
        controller: 'deletectrl',
        controllerAs: 'del',
        authenticate: true
       }
    }