angular.js - angularjs中路由的HTML5模式下的URL問題
問題描述
在angularjs的路由中使用HTML5模式,結果無法加載模版(templateUrl沒有發送http請求),而且URL中還出現的轉義。但使用標簽模式時一切OK,求大佬告知?<!DOCTYPE html><html lang='en'><head>
<meta charset='UTF-8'><base href='http://www.gepszalag.com/' ><title>Title</title><script src='http://www.gepszalag.com/wenda/angular.min.js'></script><script src='http://www.gepszalag.com/wenda/angular-route.min.js'></script><script> angular.module(’myApp’,[’ngRoute’]) .config([’$routeProvider’,’$locationProvider’,function($routeProvider,$locationProvider){$locationProvider.html5Mode(true);$routeProvider.when(’/first’,{ controller:’firstCtrl’, templateUrl:’1.html’}).when(’/second’,{ controller:’secondCtrl’, templateUrl:’2.html’}).otherwise({ redirectTo:’/first’}); }]) .controller(’myController’,function($scope,$http,$window){ }) .controller(’firstCtrl’,function($scope,$http){ }) .controller(’secondCtrl’,function($scope,$http){ });</script>
</head><body ng-app='myApp' ng-controller='myController'><p>
<a href='http://www.gepszalag.com/wenda/3466.html#/first'>first</a><a href='http://www.gepszalag.com/wenda/3466.html#/second'>second</a>
</p><p ng-view></p></body></html>
問題解答
回答1:目錄結構
模板1 - templates/1.html
<h2>First Html</h2>
模板2 - templates/1.html
<h2>Second Html</h2>
<!DOCTYPE html><html><head> <meta charset='utf-8'> <meta name='viewport' content='width=device-width'> <title>Angular Demo</title> <!--后面需根據部署后的實際路徑做調整--> <base href='http://www.gepszalag.com/' > <script src='https://cdn.bootcss.com/angular.js/1.5.11/angular.min.js'></script> <script src='https://cdn.bootcss.com/angular.js/1.5.11/angular-route.min.js'></script></head><body ng-app='myApp' ng-controller='myController'><p> <a href='http://www.gepszalag.com/first'>first</a> <a href='http://www.gepszalag.com/second'>second</a></p><p ng-view></p><script> angular.module(’myApp’,[’ngRoute’]) .config([’$routeProvider’,’$locationProvider’,function($routeProvider,$locationProvider){$locationProvider.html5Mode(true);$routeProvider.when(’/first’,{ controller:’firstCtrl’, templateUrl:’templates/1.html’}).when(’/second’,{ controller:’secondCtrl’, templateUrl:’templates/2.html’}).otherwise({ redirectTo:’first’}); }]) .controller(’myController’,function($scope,$http,$window){ }) .controller(’firstCtrl’,function($scope,$http){ }) .controller(’secondCtrl’,function($scope,$http){ });</script></body></html>
另外路由建議使用 ui-router
參考資料
Angularjs html5mode模式路由
angular路由去掉的URL里的#號
相關文章:
1. 極光推送 - Android app消息推送 百度 極光 個推 信鴿哪個好一些?2. 什么是前后端分離?用vue angular等js框架就能實現前后分離了嗎?3. ddos - apache日志很多其它網址,什么情況?4. android - 百度地圖加載完成監聽5. apache - 想把之前寫的單機版 windows 軟件改成網絡版,讓每個用戶可以注冊并登錄。類似 qq 的登陸,怎么架設服務器呢?6. java - Mybatis關聯查詢7. javascript - 求根據地址查郵編的API8. javascript - vue 初始化數據賦值報錯9. java - 為什么第一個線程已經釋放了鎖,第二個線程卻不行?10. 這是什么情況???
