$location is not defined in angularjs

In the below , roles.js file whenever the onSelect function is called if the role is UserRole i’m navigating user to different page

angular.module('myApp.roles', ['ngRoute'])
       .config(['$routeProvider', function ($routeProvider) {
           $routeProvider.when('/roles', {
           templateUrl: 'roles/roles.html',
           controller: 'PostsCtrl',
           selector: 'roles'
       $routeProvider.when('/footer', {
           templateUrl: 'footer/footer.html',
           controller: 'footerCtrl'

      .controller('PostsCtrl', ['$scope', '$rootScope', '$location', function ($scope,
        $rootScope, $log, $location) {
           $scope.onSelect = function (A, B, C) {
               localStorage.setItem("A", JSON.stringify(A));
               localStorage.setItem("B", JSON.stringify(B));
               localStorage.setItem("C", JSON.stringify(C));
               if (role.roleName === "User Role") {
                   $rootScope.roles = [];
               } else {
                   $rootScope.mangoes = JSON.parse(localStorage.getItem("B"));

I have defined $location in the controller and added it as parameter in function . When i debugged it then i found that $location is undefined. Error is :Cannot read property ‘url’ of undefined.

Source: AngularJS

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.