URL with string parameters with angular.js

Posted on

Question :

I would like to know the best way to treat url with string parameters in angular.js.

When accessed:


It would respond to a given route that is receiving nomeDaPessoa and ServicoDaPessoa as a parameter.

Another example would be:


In this case, you will receive the name of the Person!

I use Node.js with Express.js to deliver the files.


Answer :

In Express.js you can use parameters like this:

app.get('/:nomeDaPessoa?/:ServicoDaPessoa?', function(req, res){

and then get these parameters via req.params . That is:

var nome = req.params.nomeDaPessoa;
var servico = req.params.ServicoDaPessoa;


I was able to solve my problem but I do not think it’s the best way!
Based on the response of Sergio and Rafael I created an algorithm:

When accessed via URL in the node I created the route that receives these parameters and saves the id of them as follows:

router.get('/:nomeDaPessoa?/:ServicoDaPessoa?', function(req, res){
    var nome = req.params.nomeDaPessoa;
    var servico = req.params.ServicoDaPessoa;
    serviceController.getByUrl(nome, servico, function (data){
        req.session.serviceUrl = data.data[0].servico_id;
        req.session.professionalUrl = data.data[0].profissional_id;
        res.sendfile(FRONTEND_PATH + '/views/index.html');

In the angled front end I created the route to bring the correct view:

.state('service', {
  url: '/:prof/:service',
  templateUrl: function ($stateParams){
    return '/views/detail.html';
  controller: 'DetailController'

And in detailController is valid if it is coming from the click on the interface or via URL, so I go to the node and get the id’s that are in the session:

if($rootScope.service && $rootScope.service.id){ //clicou em um servico na interface, nesse caso já tenho o id dos elementos!
} else {
    Service.getSession(function(data){ //Vai ao node e pega o req.session

Well as I said I think it’s not the best way but solve my problem! if someone else has a better solution let me know! valew


Leave a Reply

Your email address will not be published. Required fields are marked *