【发布时间】:2015-07-12 20:44:20
【问题描述】:
我想AngularJS改变路径而不重新加载,看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]);
};
}]);
在控制器中:
angular.module('App')
.controller('DetailController', ['$scope', '$location', function($scope) {
$scope.changeURL = function(){
console.log("IN changeURL");
$location.path('/sample/gfshdfdsf', false);
};
}]);
如果调用changeURL,会报错:ReferenceError: $location is not defined
有人可以帮助我吗?谢谢!
【问题讨论】:
标签: javascript angularjs url-rewriting