在Angularjs删除#从URL,而在路线已经.RUN [英] Removing # from url in Angularjs while having .run in routes
问题描述
下面是AngularJS我的 app.js
路由文件
Here is my app.js
route file in AngularJS
var app = angular.module('myApp', ['ngRoute', 'ngAnimate', 'toaster']);
app.config(['$routeProvider',
function ($routeProvider) {
$routeProvider.
when('/login', {
title: 'Login',
templateUrl: 'resources/views/layouts/loginUser.php',
controller: 'authCtrl'
})
.when('/', {
title: 'Login',
templateUrl: 'resources/views/layout/login.php',
controller: 'logoutCtrl'
})
.when('/reset', {
title: 'Reset Password',
templateUrl: 'resources/views/layouts/forgetPassword.php',
controller: 'authCtrl'
})
.when('/invalidtoken', {
title: 'Login',
templateUrl: 'resources/views/layout/invalidtoken.php',
controller: 'authCtrl',
role: '0'
})
//$locationProvider.html5Mode(true);
}])
.run(function ($rootScope, $location, Data, $http) {
$rootScope.$on("$routeChangeStart", function (event, next, current) {
var nextUrl = next.$$route.originalPath;
if (nextUrl == '/signin' || nextUrl == '/login' || nextUrl == '/verify' || nextUrl == '/register' || nextUrl == '/registered' || nextUrl == '/reset' || nextUrl == '/resetdone' || nextUrl == '/registersuccess')
{
$location.path(nextUrl);
}
else
{
$location.path('/');
}
});
});
下面我用.RUN来处理几个请求。
Here i use .run to handle few requests.
我想从URL删除#使URL pretty,
I want to remove the # from the url to make the url pretty,
所以,我没有这样的去除#的建议<一href=\"http://stackoverflow.com/questions/14771091/removing-the-hashtag-from-angularjs-urls-symbol\">here
So i did like this to remove the # as suggested here
app.config(['$routeProvider', '$locationProvider'
function ($routeProvider, $locationProvider) {
和在最后一行
$locationProvider.html5Mode(true);
但什么也没有发生应用程序,它剧照贫富#在URL中。
But nothing is happening to the application, it stills haves # in the url.
即使我尝试这种方式
我怎样才能做到这一点?
How can i achieve this ?
更新:
如果我这样做
.run(function ($rootScope, $location, Data, $http, $locationProvider) {
和在最后一行
$locationProvider.html5Mode(true);
我收到此错误
Error: $injector:unpr
Unknown Provider
我已经尝试了很多办法,但他们没有工作。
I have tried in many ways, but none of them working.
更新2:
或任何人可以提出这在URL提供的例子,而不#angularjs例子的链接?
Or can anyone suggest a link of angularjs example which provides example without # in url ?
推荐答案
为什么要使用 .RUN()
?添加&LT;基本href =//&GT;
你&LT; HEAD&GT;
或你的身体开始(第一行),然后你.RUN的逻辑匹配()试试这个(。否则(/路径)
你的 $ routeProvider
)
Why do you want to use the .run()
? Add <base href="/" />
to you <head>
or start of your body (first line) and then to match the logic of your .run() try this ( .otherwise("/path")
to your $routeProvider
):
var app = angular.module('myApp', ['ngRoute', 'ngAnimate', 'toaster']);
app.config(['$routeProvider', '$locationProvider',
function ($routeProvider, $locationProvider) {
$routeProvider.
when('/login', {
title: 'Login',
templateUrl: 'resources/views/layouts/loginUser.php',
controller: 'authCtrl'
})
.when('/', {
title: 'Login',
templateUrl: 'resources/views/layout/login.php',
controller: 'logoutCtrl'
})
.when('/reset', {
title: 'Reset Password',
templateUrl: 'resources/views/layouts/forgetPassword.php',
controller: 'authCtrl'
})
.when('/invalidtoken', {
title: 'Login',
templateUrl: 'resources/views/layout/invalidtoken.php',
controller: 'authCtrl',
role: '0'
})
. otherwise("/");
$locationProvider.html5Mode(true);
}]);
如果您仍然面临着问题,我建议 https://github.com/angular-ui/ UI路由器
If you still face issues, I recommend https://github.com/angular-ui/ui-router
更新:
您 index.html的
<!DOCTYPE html>
<html lang="en" ng-app="myApp">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Green Hopping</title>
<link rel="shotcut icon" type="favicon.ico" href="public/images/favicon.ico" />
<link rel="icon" type="favicon.ico" href="public/images/favicon.ico" />
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.2/angular.min.js"></script>
<script src="https://code.angularjs.org/1.4.2/angular-route.min.js"></script>
</head>
<body ng-cloak="">
<base href="/">
<div data-ng-view="" id="ng-view" class="slide-animation"></div>
</body>
<script>
var app = angular.module('myApp', ['ngRoute']);
app.config(['$routeProvider', '$locationProvider',
function ($routeProvider, $locationProvider) {
$routeProvider.
when('/', {
title: 'Home',
templateUrl: 'home.html',
controller: 'homeCtrl'
})
.when('/login', {
title: 'Login',
templateUrl: 'login.html',
controller: 'authCtrl'
})
.when('/logout', {
title: 'Logout',
templateUrl: 'logout.html',
controller: 'logoutCtrl'
})
.when('/dashboard', {
title: 'Dashboard',
templateUrl: 'dashboard.html',
controller: 'dashboardCtrl'
})
.otherwise('/');
$locationProvider.html5Mode(true);
}])
.run(function ($rootScope, $location, $http, loginSrv) {
$rootScope.$on("$routeChangeStart", function (event, next, current) {
var nextUrl = next.$$route.originalPath;
var orUseUrl = $location.path();
console.log(nextUrl);
if (nextUrl === '/logout'){loginSrv.logout();}
if (nextUrl === '/login'){loginSrv.login();}
if (loginSrv.loggedin === false) { $location.path('/'); }
else { $location.path(nextUrl); }
});
});
app.service("loginSrv",function(){
var ls= this;
ls.loggedin = false;
ls.logout = function(){
ls.loggedin = false;
}
ls.login = function(){
ls.loggedin = true;
}
});
app.controller("homeCtrl",function($scope, loginSrv){
$scope.loggedin = loginSrv.loggedin;
})
app.controller("dashboardCtrl",function($scope, loginSrv){
$scope.loggedin = loginSrv.loggedin;
})
app.controller("authCtrl",function($scope, loginSrv){
$scope.loggedin = loginSrv.loggedin;
})
app.controller("logoutCtrl",function($scope, loginSrv){
$scope.loggedin = loginSrv.loggedin;
})
</script>
</html>
所有其他模板相同这样。复制粘贴 home.html的
,的login.html
以下, dashboard.html
, logout.html
。 Plunker将不能够显示与用于客户端的路由的问题。尝试这个。这完全是功能code。
All other templates are same like this. Copy paste the following for home.html
, login.html
, dashboard.html
, logout.html
. Plunker will not be able to show issues with routes for client side. Try this. This is completely functional code.
<div>
<div><a href="/">Home</a> |
<a href="/login">Login</a> |
<a href="/dashboard">Dashboard</a> |
<a href="/logout">Logout</a></div>
<div> This is from the home/login/dashboard/logout Controller. Loggedin: {{loggedin}}</div>
</div>
这篇关于在Angularjs删除#从URL,而在路线已经.RUN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!