Reputation: 55
How to rename the URL for url: '/add/{id}'
to url: '/add'
in URL. I don't want to show my Id in the URL. How to remove the id from url in AngularJs?
My Code:
.state('app.useredit', {
url: '/user-edit/:userId',
data: {
auth: true
},
views: {
'main@app': {
template: '<user-edit></user-edit>'
}
},
Upvotes: 0
Views: 357
Reputation: 27232
You can create a service to set
and get
the id
and then inject that service in the controller to get that id.
Service :
app.service('setGetId', function() {
var id = '';
getId: function() { return id; },
setId: function(requestId) { id = requestId; }
});
Controllers :
app.controller('myCtrl1', ['setGetId',function(setGetId) {
// To set the id from the one controller
setGetId.setId(id);
}]);
app.controller('myCtrl2', ['setGetId',function(setGetId) {
// To get the id from the another controller
setGetId.getId();
}]);
Upvotes: 0
Reputation: 777
To not expose the id in the URL, use a service to hold "id" inside and grab it via the controller.
Here is a related answer: "Hide param value (guid) in an URL"
Upvotes: 0
Reputation: 12103
You need to use params
:
Like this:
.state('app.useredit', {
url: '/user-edit',
params: {
userId:null
}
data: {
auth: true
},
views: {
'main@app': {
template: '<user-edit></user-edit>'
}
},
And,
$state.go('app.useredit',{userId:'u123'})
Upvotes: 1