allenylzhou
allenylzhou

Reputation: 1461

How to get route parameter using angular js?

I am hitting a REST endpoint at

www.example.com/resource/id

What is the best way for me to extract id from an angular module controller? I have looked into using $routeProvider

$routeProvider.when('/resource/:id', {}).otherwise(...)

This will only work if I set

$locationProvider.html5mode(true)

If it is not set to true, it always ends up in the otherwise clause when I hit www.example.com/resource/8

How can I get this to work even if I don't set $locationProvider.html5mode to true?

Upvotes: 0

Views: 66

Answers (3)

Mukund Kumar
Mukund Kumar

Reputation: 23211

just replace following code:

 $routeProvider.when('/resource/:id', {}).otherwise(...)

with the:

 $routeProvider.when('resource/:id', {}).otherwise(...)

/resource/:id ths take absolute url pattern. and resource/:id take relative url pattern.

so i think this will help you without setting of html mode true.

Upvotes: 0

Kalhan.Toress
Kalhan.Toress

Reputation: 21901

use www.example.com#/resource/8 instead www.example.com/resource/8

Upvotes: 0

yelvert
yelvert

Reputation: 101

I think you're looking for $route.current.params, I use angular-ui's ui-router ui-router, in which case you would use $stateParams.

Upvotes: 1

Related Questions