How to get parameters in the URL through AngularJS

5

asked by anonymous 28.10.2016 / 18:08

2 answers

3

Assuming that this 2 was defined as a route variable

.state('produtoView', {
    url: '/produto/:id' ...

You can redeem this id variable by injecting $stateParams into your controller and taking the value of the route variable by its name $stateParams.id .

Take a look at the ui-router documentation, just below it speaks of a new way of searching for the variable in the new versions of the ui-router, so the $stateParams is deprecated.

I did not put it in the new way because I did not know it either.

    
01.11.2016 / 01:17
3

Use the $location service.

For example, for a given URL http://example.com/#/some/path?foo=bar&baz=xoxo :

var abUl = $location.absUrl();     // => "http://example.com/#/some/path?foo=bar&baz=xoxo"
var url  = $location.url();        // => "/some/path?foo=bar&baz=xoxo"
var prot = $location.protocol();   // => "http"
var host = $location.host();       // => "example.com"
var path = $location.path();       // => "/some/path"
var srch = $location.search();     // => {foo: 'bar', baz: 'xoxo'}

In your specific case, $ location.path () will return /app/produto/2 .

Source.

    
28.10.2016 / 18:18