2

Right now I'm calling below code :

http://localhost:8081/cgi/#/home

And it takes me to my home page.

My app.js is :

angular.module('myModule', 
    ['ngRoute', 'ngCookies', 'ui.bootstrap', 
     'angularUtils.directives.dirPagination'])
.config([ '$routeProvider', function($routeProvider) {

$routeProvider.when('/index', {
    controller : 'homeController',
    templateUrl : './app/views/index.html',
})

.when('/', {
    controller : 'homeController',
    templateUrl : './app/views/home.html'
})

.otherwise({
    redirectTo : '/'
});
}])

Now I need to add extra parameter "debug", which I can store in my controller and if it is there I need to call some function.

I have tried adding below to my app.js

    .when('/debug', {
    controller : 'homeController',
    templateUrl : './app/views/home.html',
    })

and below line to my controller

$scope.debug = $routeParams.debug === 'true' ? true : false;
console.log($scope.debug);

in my controller :

var app = angular.module('myModule');
app.controller('homeController', function($scope, $http, $route) {
    $scope.debug = $routeParams.debug === 'true' ? true : false;
    console.log($scope.debug);
}

Also please include url you think would work in answer as I can understand from where to start looking for either routeProvider or $location functionality

But page has now stopped loading and I don't have any clue how can I make it work. Please help

1 Answer 1

1

You need to inject the $routeParams service to the controller. Just add it as a parameter to the controller function.

var app = angular.module('myModule');
app.controller('homeController', function($scope, $http, $route, $routeParams) {
    $scope.debug = $routeParams.debug === 'true' ? true : false;
    console.log($scope.debug);
}

--- Update ---

Path

Use $location.path() === '/debug' to check if the current path is '/debug'. You need to inject the $location service.

var app = angular.module('myModule');
app.controller('homeController', function($scope, $http, $route, $location) {
    $scope.debug = $location.path() === '/debug';
    console.log($scope.debug);
}

Query Param

This will allow you to check if there is a query parameter keyed "debug" with the value of "true".

http://localhost:8081/cgi/#/home?debug=true

 

var app = angular.module('myModule');
app.controller('homeController', function($scope, $http, $route, $location) {
    $scope.debug = $location.search().debug;
    console.log($scope.debug);
}
Sign up to request clarification or add additional context in comments.

6 Comments

console.log("$routeParams.debug); Above gives undefined. How can I access debug as query parameter true or false? Should i use localhost:8081/cgi/#/debug/true and .when('/debug/true', { ... }
Oh, wait. I misunderstood what you were trying to do. Debug won't be passed in as a parameter because it is not setup in app.config properly. If all you want to do is check if the current path is /debug in the homeController, you can use the $location service.
@SiddharthShah Updated.
update worked. Thanks, also can you tell me if i want to add or call below url what should i do ? localhost:8081/cgi/#/home?debug=true or localhost:8081/cgi/#home?debug
@naiveTechie Updated my answer
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.