简体   繁体   中英

Why is $location not accessible in this controller?

As said in the question, $location service is not accessible in the controller at the debugger point.

$(document).ready(function() {
    return angular.module('dashboard.controllers').controller(
    'projectController', 
    ['$route', '$routeParams', '$scope', '$location', '$rootScope',
    'ProjectDataService', 'UserService', 

    function($route, $routeParams,$scope, $location, $rootScope,
    ProjectDataService, UserService) 
    {
        return ProjectDataService.loadProjectData().then(function(d) {
            $scope.projectSearch = "";
            $scope.projects = ProjectDataService.getProjectData();
            debugger;
        });
    }
  ]);
});

The culprit here is Closures. I would suggest taking a quick look at Mozilla's MDN article on the topic .

In your case, the anonymous function passed to then() is the closure. Since you haven't used $location in the scope of your anonymous function, it isn't available when the anonymous function is executed. Therefore, you are unable to see the value in the debugger.

If you were to use $location inside the scope of the anonymous function, you would then see it available inside the debugger as it would be in scope.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM