Chester Lim Chester Lim - 3 months ago 11
AngularJS Question

Adding data from object to url in $stateprovider

I want to add the id i get from

$state.go('state1', {obj: {id: request.id, title: request.title}}


to the URL in

angular.module('app')
.config(function ($stateProvider) {
$stateProvider
.state('state1', {
url : '/edit',
params: {obj: null},
...
});
});


I accessed the id in my Controller using

vm.id = $stateParams.obj.id;


and tried adding it to the url by trying

url : '/edit/:id'
url : '/edit/:obj.id'


I tried searching for a while but it's either they are passing params (not object) or they are passing an object but uses a static url. I'm new to Angular can someone point out what i'm missing? Thanks.

Answer

The parameter in the url needs to match what you're passing in. Try this:

$stateProvider
      .state('state1', {
        url : '/edit/:id',
        params: {obj: null},
        ...
      }); 

Then call using:

$state.go('state1', {id: request.id, obj: {id: request.id, title: request.title}}