Javascript angular module.run ReferenceError: $location 未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30013618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
angular module.run ReferenceError: $location is not defined
提问by UNSTABLE
I want to AngularJS Change Path Without Reloading, look http://joelsaupe.com/programming/angularjs-change-path-without-reloading/
我想在不重新加载的情况下更改 AngularJS 路径,请查看http://joelsaupe.com/programming/angularjs-change-path-without-reloading/
in core.js:
在 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]);
};
}]);
In controller:
在控制器中:
angular.module('App')
.controller('DetailController', ['$scope', '$location', function($scope) {
$scope.changeURL = function(){
console.log("IN changeURL");
$location.path('/sample/gfshdfdsf', false);
};
}]);
If invoke changeURL, it will occur error:ReferenceError: $location is not defined
如果调用changeURL,会出现错误:ReferenceError: $location is not defined
Can somebody help me? Thanks!
有人可以帮助我吗?谢谢!
回答by sol4me
$location is not injected in the controller, so just change
$location 没有注入到 中controller,所以只需更改
.controller('DetailController', ['$scope', '$location', function($scope)
to
到
.controller('DetailController', ['$scope', '$location', function($scope, $location)
回答by Andreas Panagiotidis
I was getting the same error and I deleted the $rootScope from the definitions. After that it worked. No idea why.
我遇到了同样的错误,我从定义中删除了 $rootScope。在那之后它起作用了。不知道为什么。
Not working
不工作
app.factory("OrganizationService",
['$q', '$http', '$log', '$location', '$rootScope', '$timeout', 'LoadSubscriptionsService', 'LoadRolesService',
function($scope , $http, $log, $location, $cookies, $rootScope, $timeout) {
Working
在职的
app.factory("OrganizationService",
['$q', '$http', '$log', '$location', '$timeout', 'LoadSubscriptionsService', 'LoadRolesService',
function($scope , $http, $log, $location, $cookies, $timeout) {

