Temp O'rary
Temp O'rary

Reputation: 5826

How am I getting values for routeParams, path and search without injecting $routeParams or $location?

Please see a sample code below:

var app = angular.module("sampleroute", ["ngRoute"]);    
app.config(function ($routeProvider) {
    $routeProvider
        .when("/", {
            templateUrl: "main.htm",
            controller: "mainCtrl"
        })
        .when("/page/:type", {
            redirectTo: function (routeParams, path, search) {
                console.log(routeParams);
                console.log(path);
                console.log(search);
                return "/";
            }
        })
        .otherwise({
            redirectTo:"/"
        });
    //console.log(routeParams, path, search);
});

The following code prints out 3 values for routeParams, path and search when routed to "/page/types?test=testValue". So this works without any issue.

My question is without injecting $routeParams, I am getting the value of routeParams and without injecting $location I am getting the value of path and search.

How is this happening?

Upvotes: 0

Views: 573

Answers (1)

nweg
nweg

Reputation: 2865

Take a look at the angular documentation, it states:

If redirectTo is a function, it will be called with the following parameters:

{Object.} - route parameters extracted from the current $location.path() by applying the current route templateUrl.

{string} -current $location.path()

{Object} - current $location.search()

Angular is doing this behind the scenes for you. That is why you don't need to inject $location or $routeParams directly.

Here is a link to the docs: https://docs.angularjs.org/api/ngRoute/provider/$routeProvider

Upvotes: 1

Related Questions