Pass optional parameter to state with $state.go


#1

Hi,

I want to use same form for creating new entry and update existing entry. To do this I want to pass optional parameter, entryId via $stateProvider.

I have below code where I want to implement this, but this is not working for me, as if I don’t pass entryId it always take me to the default route.

.state(‘newentry/:entryId’, {
url: “/new-entry/:entryId”,
templateUrl: “new-entry.html”,
controller: ‘NewEntryCtrl’
})

Please suggest me how I can achieve this.


#2

You do not need entryId placeholder in state name, only in url
.state(‘newentry’, {
url: “/new-entry/:entryId”,
templateUrl: “new-entry.html”,
controller: ‘NewEntryCtrl’
})

You can go to this state with
$state.go(“newentry”, {“entryId”:123});


#3

Thanks but that’s not working for me.

Actually I want to access New Entry screen by <a href="#/new-entry">New Entry</a> and Edit Screen by $state.go('newentry/:entryId',{entryId:123});. But it always redirect to default route.

As of now I have found a work around below, but would like to know correct way to handle this to avoid any duplicate code.

.state('addcategory', {
      url: "/add-category",
      templateUrl: "add-category.html",
      controller: 'AddCategoryCtrl'
    })
	.state('editcategory', {
      url: "/edit-category",
      templateUrl: "edit-category.html",
      controller: 'EditCategoryCtrl'
    })