URL with string parameters with angular.js

Asked

Viewed 2,832 times

3

I would like to know how to best treat url with parameters string angular.js.

When accessed:

www.app.net/nomeDaPessoa/ServicoDaPessoa

Would respond to a certain route that is receiving the nomeDaPessoa and ServicoDaPessoa as a parameter.

Another example would be:

www.app.net/nomeDaPessoa

In this case you would receive as parameter!

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

  • I found something interesting to implement what you need: [http://www.codeproject.com/Articles/806029/Getting-started-with-AngularJS-and-ASP-NET-MVC-Par] More pro final he explains the route handling

  • Interesting this option Rafael, however I would like to not use # in the url! I tried it with ui-route but it uses # in the url!

  • Fábio found something else I hope to help: [http://javascriptbrasil.com/tag/angularjs/] or [http://tableless.com.br/criando-uma-aplicacao-single-page-com-angularjs/]

  • The first link pointed me a way, I will test and if it goes right put here the solution! Valew Rafael

  • You activated the Html5Mode on the router?

  • I don’t think @mutlei!

  • To activate, you make use of the line $locationProvider.html5Mode(false); with the appropriate boolean, in module.config of $locationProvider.

  • He solved the question Fabius?

Show 3 more comments

2 answers

1

In the Express.js you can use parameters this way:

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

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

var nome = req.params.nomeDaPessoa;
var servico = req.params.ServicoDaPessoa;
  • Valew @Sergio, but can you tell me how to solve this route in Algular.js? I’ve been looking at https://github.com/angular-ui/ui-router but I couldn’t find an answer to that question!

1

I managed to solve my problem but I don’t think it’s the best way! Based on the response of Sergio and Rafael I created an algorithm:

When accessed via URL in Node created the route that receives these parameters and saved in session the id of them so:

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');
    });
});

At the front end at the angle I created the route that to bring the correct view:

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

And in detailController valid if this coming from the click on the interface or via URL, with this 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!
    $scope.getDetailService($rootScope.service.id);
} else {
    Service.getSession(function(data){ //Vai ao node e pega o req.session
        $scope.getDetailService(data.servico_id);
    });
}

Good as I said I think it’s not the best way but solve my problem! if anyone has another solution better I warn me! Valew

Browser other questions tagged

You are not signed in. Login or sign up in order to post.