Janbango
Janbango

Reputation: 191

Angularjs $routeParams issue

I am struggling to get $routeParams to work. What I want to do is populate an unsubscribe field with an email address that is contained in a link to a url. I am using AngularJS and after reading around the subject is seemed $routeParam was the way to go but I cannot get it to work

My HTML:

        <form class="form-signin" name="regForm">
                 <input type="email" id="email" name="email" class="form-control" ng-model="user.email">
       </form>

My .config with $routeProvider

    .config(['$routeProvider',  function ($routeProvider) {
$routeProvider
     .when('/unsubscribe/:email', {
         templateUrl: 'unsubscribe/unsubscribe.html',
         controller: 'unsubscribeCtrl'
     })
 }]);

My .controller with $routeParams

.controller('unsubscribeCtrl', ['$scope', '$location', '$routeParams', function ($scope, $location, $routeParams) {

   var param1 = $routeParams.email

   console.log(param1)
 }]);

Now when I navigate to myURL/app/#/unsubscribe?email=some_text the browser remains blank and I do not get any errors in the console. Am I mis-understanding how $routeParams is supposed to work?

Upvotes: 0

Views: 97

Answers (3)

rayners
rayners

Reputation: 539

$routeParams is populated by the named parameters embedded in the path (e.g., in /unsubscribe/:email, :email is the parameter that will be extracted).

On top of that, the router tries to match the path portion of the url (without the ?…) to the list of known routes, which in this case only includes /unsubscribe/:email.

So, to make the email parameter available to the controller in $routeParams, you should point your browser at myURL/app/#/unsubscribe/some_text instead.

Upvotes: 0

DB.Null
DB.Null

Reputation: 1194

The url you are trying to access doesn't match the given route in $routeparams table. You should try to navigate app/#/unsubscribe/some_text. The $routeparams will understand that email is some_text

Upvotes: 1

Pankaj Parkar
Pankaj Parkar

Reputation: 136134

As your URL is configured in router, your URL in browser should be below, then only you can get email value as some_text from $routeParams

myURL/app/#/unsubscribe/some_text

Upvotes: 0

Related Questions