angular module.run ReferenceError:$ location未定义

UNS*_*BLE 11 javascript url-rewriting angularjs

我想AngularJS Change Path Without Reloading,请查看http://joelsaupe.com/programming/angularjs-change-path-without-reloading/

在core.js中:

 'use strict';
    angular.module('App',['ngRoute'])
        .run(['$route', '$rootScope', '$location', function ($route, $rootScope, $location) {
        var original = $location.path;
        $location.path = function (path, reload) {
            if (reload === false) {
                var lastRoute = $route.current;
                var un = $rootScope.$on('$locationChangeSuccess', function () {
                    $route.current = lastRoute;
                    un();
                });
            }
            return original.apply($location, [path]);
        };
    }]);
Run Code Online (Sandbox Code Playgroud)

在控制器中:

    angular.module('App')        
        .controller('DetailController', ['$scope', '$location',  function($scope) {
  $scope.changeURL = function(){
            console.log("IN changeURL");
            $location.path('/sample/gfshdfdsf', false);
        };      
    }]);
Run Code Online (Sandbox Code Playgroud)

如果调用changeURL,则会发生错误:ReferenceError: $location is not defined

有人能帮助我吗?谢谢!

sol*_*4me 28

$ location没有注入controller,所以只需改变

.controller('DetailController', ['$scope', '$location',  function($scope)
Run Code Online (Sandbox Code Playgroud)

.controller('DetailController', ['$scope', '$location',  function($scope, $location)
Run Code Online (Sandbox Code Playgroud)