Custom Routes in UI Router Angular JS (URL Shortcuts) - angularjs

I have some states which look like this:
.state('patient.patient-login', {
abstract: true,
template: '<ui-view></ui-view>'
})
.state('patient.patient-login.practiceID', {
url: "/patient-login/:practiceID",
scope: true,
templateUrl: "views/patient-login.html"
})
The problem is the :practiceID variable can be quite long and not user friendly, I want to give a user the ability to create a custom URL which is tied to the practice ID, for example on Facebook when you create an account you start out with a profile URL like this: http://facebook.com/123456789 but then you can change it to http://facebook.com/whateveryouwant
So essentially I want to accomplish something like this:
POINTER
http://domain.com/companyname > http://domain.com/#/patient-login/companyid
ANOTHER EXAMPLE
http://domain.com/ladentist > http://domain.com/#/patient-login/-85k3jfGEioltold
I was able to allow a user to set their unique url but I'm not sure how to even start to setup the routing like that?

Not sure what the rest of your project looks like but you could update your state to handle both id's and unique urls in the resolve property. Which would roughly look like this:
.state('patient.patient-login.practiceID', {
url: "/patient-login/:practice",
scope: true,
templateUrl: "views/patient-login.html",
resolve: {
patientData: [
'$stateParams',
'practiceSvc', // replace practiceSvc with however you are fetching data from your server
function($stateParams, practiceSvc) {
// check if the :practice param is an id or unique url
if ($stateParams.practice === /* id check */) {
// Call to API - get practice by ID
return practiceSvc.getByID($stateParams.practice);
} else {
// Call to API - get practice by unique url
return practiceSvc.getBySlug($stateParams.practice);
}
}
]
}
})
This assumes you can tell the difference between a unique url and an id. If you can't do that you could update it to check for the id first then the unique url if no practice is found.

Related

Populate route params with resolved data in angular ui-route

Give the following code:
$stateProvider
.state('app.users', {
abstract: true,
url: '/users',
templateUrl: 'views/users.html',
resolve: {
userId: Users => Users.lastUserSelected()
},
})
.state('app.users.questions', {
url: '/:userId/questions',
templateUrl: 'views/questions.html',
controller: 'UsersQuestionsCtrl',
resolve: {
questions: (userId) => Questions.getAll(userId)
}
})
When I transit to the app.users.questions state, everything goes ok, but the URL in the browser ends up being like mydomain.com/users//questions (notice the double forward slash // in the route, there's no userId)
Seems like the user ID resolved from the parent state is not populated in child route, or something alike. I think I'm missing a key concept here.
I would like to achieve a URL like mydomain.com/users/USERID/questions.
I've been through many ui-router guides but couldn't figure this out, any help is really appreciated.
I'm populating my sideBar menu using the addMenuItem and addMenuSubItem methods from the MEANJS project.
Menus.addMenuItem('sidebar', {
state: 'app.users',
type: 'dropdown',
iconClass: 'fa fa-comments',
position: 4,
roles: ['*']
});
Menus.addSubMenuItem('sidebar', 'app.users', {
state: 'app.user-questions'
});
Notes:
1- It doesn't matter the previous state, wherever state I come from... it's not related at all to the user, so passing the :userId from all the possible previous states sounds wrong to me. I'm just using USERS and QUESTIONS as an example here but those are not the entities i'm working with in my actual project.
2 - Im using MEANJS seed project

Angular states are confused which controller to use

I have two states on my angular app.
First :
.state('profile', {
url: '/:user/:user_id',
templateUrl: 'modules/profile/templates/profile.html',
controller:'profileController',
showClose : 'false',
resolve: {
USERNAME : function($stateParams){
return $stateParams.user;
},
USERID : function($stateParams){
return $stateParams.user_id;
}
}
})
Second :
.state('sub-moments-everyday', {
url: '/moments-everyday/:moment',
templateUrl: 'modules/moments-everyday/templates/ME-submoments.html',
controller:'subMomentsEverydayController',
})
Whenever I typed a valid user and user_id on the profile state there are no problems. But whenever I type /moments-everyday/randomstring it seems like the ui-router is getting confused. It is using the controller and template for the profile state.
Please help. Thanks.
Have you tried checking the order in which the states are placed in the file? /:userid/:username checks for ANY URL that matches that pattern. That pattern contains two variable parameters so when you try and access /moments-everyday/:moments, it sees the /:userid/:username pattern and calls that state.
Try making sure you have your /moments-everyday/:moments pattern BEFORE the /:userid/:username pattern. UI-router starts at the top of the file and checks each pattern going down the file till it finds the first matching pattern.

confirm email address using Angularjs

The backend has created user and needs the email to be confirmed and it has sent out an to the address (see below)
Please confirm your account by clicking
after clicking on the link the user goes to the frontend I have the following route setup using ui.router:
.state('confirmemail', {
url'/confirmemail',
templateUrl: 'client/view/confirmEmail.html' })
how do I get this to route to this state using the above link and how do I access the user and code?
thanks
You can use stateParams:
In the email:
Link
In your config:
.state('confirmemail', { url'/confirmemail/:id', ... })
In your controller:
var myId = $stateParams.id;
// Then lookup user with myId
The link the user clicks on should go to yoursite/confirmemail. Simple as that, just have them navigate to the route that matches the url in your state.
As for passing the information, I can think of a couple options. You could pass a querystring, yoursite/confirmemail?userId=123 or whatever, or you could set up your route to pass some sort of id (or whatever the information may be). Either would have basically the same result. Again, you would define this in your email link then capture it when that path is hit.
Example of passing id using angular:
.state('confirmemail', {
url: '/confirmemail/:id',
resolve: {
id: function ($stateParams) {
return $stateParams.id;
}
}
}
and set the link to yoursite/confirmemail/123 where 123 is the user id.
From there you can inject the id into a controller.

how to change the part of url if condition will match in ui-router?

I have two kinds of states in my $stateProvider which are authenticated user access url and public access url..
I have some sort of urls in public access , the thing is i need to prevent these url to access of authenticated user and need to change the url with another one.
Example Suppose,
http://localhost:3000/#/pjobadd/1 is public access url,
if authenticated user will access this url i need to change as http://localhost:3000/#/jobadd/1 .
I'm trying to take the solution as bellow
i have attached parameter in state provider like
.state('admin-panel.public.jobadd', {
url: '/pjobadd/:jobID',
templateUrl: 'app/public/jobadd.tmpl.html',
controller: 'PublicJobaddController',
resolve: {
jobAdd: ['Public', '$stateParams', function(Public,$stateParams) {
return Public.jobAdd($stateParams.jobID);
}]
},
data: {
requireChange: 'pjobadd'
}
})
and used that requireChange in app.js as following
$rootScope.$on('$stateChangeStart', function (event, toState, toParams) {
if (!Auth.isAuthenticated()){
var requireChange = toState.data.requireChange;
console.log(requireChange);
switch(requireChange){
case 'pjobadd':
$state.go('admin-panel.default.jobadd');
case 'psearch':
$state.go('admin-panel.default.search');
}
There are issues i need to append the url passed parameters and $state.go() also not redirecting to the mentioned url.
i don't think that it'l be the efficient way.
Can anyone suggest a way to it???
check this url ...
How to pass parameters using ui-sref in ui-router to controller
can you provide your code here?and give me some more clear information about your requirement..

Angular-UI ui-router supporting infinitely nested states

I have a situation in my Angular app where I'd like to consider an infinitely nested state/route. For example, let's say that I have a person and I want to see the lineage of the family. I might have routes that look like this:
/person/0
/person/0/child/1
/person/0/child/1/child/2
/person/0/child/1/child/2/child/3
And this could go on forever, as the user click further down the tree. How does a route like this get defined using ui-router?
You can use regex to define your route and then parse the url to get the child id, like this:
$stateProvider.state('person', {
url: '/person/:id',
views: {
'': {
templateUrl: 'parentTemplate.html',
controller: 'ParentCtrl'
}
}
}).state('child', {
url: '/person/:id/{path:.*}',
templateUrl: function(params) {
// params.path contains something like `child/1/child/2`
// so you get get the current child id like this
var path = params.path.split('/').reverse(),
childId = path[0];
// for more flexibility you can retrieve all ids from the request and store them in $stateParams
// so you can easily work with them in your controller
// here you can write some logic to determine the right template
return 'childTemplate.html';
},
controller: 'ChildCtrl'
});

Resources