0

Here's my url

localhost/project/#/showprofile/18

I want to display the parameter 18 in my view

In the app.js i have

.when('/showprofile/:UserID', {
     title: 'Show User Profile',
     templateUrl: 'views/layout/showprofile.php',
     controller: 'authCtrl',
     })

Here the page showprofile.php is displaying, but suddenly the url goes like this

localhost/project/#/showprofile/:UserId

How can i get the value 18 inside the showprofile.php and make the url as it is i.e.,

localhost/project/#/showprofile/18

1
  • you will get the user id with this $routeParams.UserID. pass it to the controller and assign it to a variable and you can use it in the tamplate Commented Jul 16, 2015 at 5:08

3 Answers 3

2

Use $routeParams to get the parameter value:

$routeParams.UserID

Make sure you inject $routeParams before using it.

EDIT How can i get the value 18 inside the showprofile.php

<?php
    $link = $_SERVER['PHP_SELF']; // Get current URL
    $link_array = explode('/', $link); // Split by /
    echo $page = end($link_array); // Get last element from array

?>
Sign up to request clarification or add additional context in comments.

5 Comments

What might be reason for redirection to localhost/project/#/showprofile/:UserId this ?
I am not sure about inject whenever i click the link i am getting redirected to the above url, is there any problem in my route ?
Thanks for the update, but ii don't know the reason for the immediate redirection to here after i click the link localhost/project/#/showprofile/:UserId
@user4989228 It is what you've told to do, by creating a route
I should have like this ? .when('/showprofile/$routeParams.UserID', { ? I am getting error in console like this TypeError: Cannot read property '$$route' of undefined
1

You can use $location.path().

As an example,

if ($location.path() === 'localhost/project/#/showprofile/18' 
{
// logic goes here
}

You need to inject $location

4 Comments

You mean instead of .when i want to use the given if condition ?
you can use .when and not necessary if.. but the point is $location.path() gives url value.
But how can i fix the url redirection to this ? localhost/project/#/showprofile/:UserId
Your question title: How to get the url param in angular js? please correct the title.
-1

You can use jquery to get value.

var parts = window.location.pathname.split('/');
var id = parts[parts.length - 1];

3 Comments

But i said i my url is getting redirected to this localhost/project/#/showprofile/:UserId :(
Your quesion: How to get the url param in angular js?
Ok, but i have stated that i am getting redirected to the above given url ! If i use the one you have given i will always get :UserId as my result

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.