Angular 1 - get current URL parameters

前端 未结 6 1233
情话喂你
情话喂你 2020-11-30 03:37

I want to extract data from current URL and use it in controller. For example I have this url:

app.dev/backend/surveys/2

Bits t

6条回答
  •  庸人自扰
    2020-11-30 04:29

    In your route configuration you typically define a route like,

    .when('somewhere/:param1/:param2')
    

    You can then either get the route in the resolve object by using $route.current.params or in a controller, $routeParams. In either case the parameters is extracted using the mapping of the route, so param1 can be accessed by $routeParams.param1 in the controller.

    Edit: Also note that the mapping has to be exact

    /some/folder/:param1
    

    Will only match a single parameter.

    /some/folder/:param1/:param2 
    

    Will only match two parameters.

    This is a bit different then most dynamic server side routes. For example NodeJS (Express) route mapping where you can supply only a single route with X number of parameters.

提交回复
热议问题