user7397787
user7397787

Reputation: 1480

Get Id from url() in angular

How to get id from url()
This is my url

http://localhost:59113/Project/EditProject?id=2

I did it using jquery..

var url = document.URL;
        var id = /id=([^&]+)/.exec(url)[1];
        var result = id ? id : ' ';

But I need to do it using angular.Can anyone help me?
I tried this too(updated part)

 var app = angular
                .module("intranet_App", [])
                    app.config("$routeProvider", function ($routeProvider) {
                        $routeProvider.when('/view1/:param1/:param2', {
                            templateUrl: 'Project/EditProject.html',
                            controller: 'myCtrl'
                        })
                    })
                    .controller("myCtrl", ["$scope", "$http", "$location", "$route",  function ($scope, $http, $location, $route) {
                    // alert($location.id)
                    var a = $route.current.params;
                    alert(a.id)
                }])

Upvotes: 2

Views: 4042

Answers (3)

Ram_T
Ram_T

Reputation: 8484

Using $routeParams you can get id,

Updated answer

Use $route instead of $routeParams. Note that the $routeParams are only updated after a route change completes successfully. This means that you cannot rely on $routeParams being correct in route resolve functions. Instead you can use $route.current.params to access the new route's parameters.

$route.current.params

EDIT

You must do this in .config() method

app.config(function($routeProvider){
      $routeProvider.when('/view1/:param1/:param2', {
            templateUrl: 'Project/EditProject.html',
            controller: 'myCtrl'
      })
})

Upvotes: 3

Madhvesh S
Madhvesh S

Reputation: 139

$location.search() will return an object of key-value pairs. in your case:You could access this value directly with $location.search().id

Upvotes: 0

selvakumar
selvakumar

Reputation: 654

$location.search() returns an object, so as per above url

http://localhost:59113/Project/EditProject?id=2

$location.search().id will fetch you an answer.

More Info:

If you are getting an empty object with $location.search(), it is probably because Angular is using the hashbang strategy, To fix this you need to change the following in config file

$locationProvider.html5Mode(true);

Upvotes: 0

Related Questions