I want to get the id from the url http: // localhost / Inventory / Details / 4b72c58e-ec01-4cc7-aa84-0763d9f9aad2 in the controller. I use $ stateProvider, but $ stateParams.id is always empty. Here is my module:

'use strict'; (function (angular) { angular.module('inventory', [ 'ngMaterial', 'common', 'ui.router' ]).config(configuration); configuration.$inject = ['$stateProvider']; function configuration($stateProvider) { $stateProvider .state('inventory.details', { url: '/Inventory/Details/:id', templateUrl: '/app/inventory/inventory.html', controller: 'InventoryController' }); } }(angular)); 

Here is the controller:

 (function() { 'use strict'; angular.module('inventory').controller('InventoryController', InventoryController); InventoryController.$inject = ['$stateParams']; function InventoryController($stateParams) { var id = $stateParams.id; 

UPD The problem was in the controller declaration:

 <div ng-include="'/app/inventory/inventory.html'" ng-controller="InventoryController as vm"></div> 

After replacing with:

 <div ui-view="page"></div> 

everything is working

  • But in general, everything starts? no errors in the console? - Grundy
  • yes, it works. no errors. - Alena Shlykova
  • Can you show the code where you put the parameter when switching to the inventory.details state? - Max
  • Added to the response code with which you can send the parameter to your state. - Max
  • one
    Make a complete example that you can run. - Qwertiy

1 answer 1

The transition to another state should be something like this:

 function go(detailId) { $state.go('inventory.details', { id: detailId }); } 

Here in the go function you pass the id you need and it will be passed to your inventory.details

You must specify the parameter explicitly in the stack.

 function configuration($stateProvider) { $stateProvider .state('inventory.details', { url: '/Inventory/Details/:id', templateUrl: '/app/inventory/inventory.html', controller: 'InventoryController', params: { id: null } }); } 
  • No no need. It is not necessary - Grundy
  • I tried to add, but the result is still the same - Alena Shlykova
  • A couple of days ago there was a similar problem in the ionic application. I was helped by an explicit indication of the parameter in the stack. - Max
  • @Grundy I apologize, yes you are right, if the parameter is specified in the url, you can omit it in params. - Max