angular.js - angularjs中路由的HTML5模式下的URL問題
問題描述
在angularjs的路由中使用HTML5模式,結果無法加載模版(templateUrl沒有發送http請求),而且URL中還出現的轉義。但使用標簽模式時一切OK,求大佬告知?<!DOCTYPE html><html lang='en'><head>
<meta charset='UTF-8'><base href='http://m.4tl426be.cn/' ><title>Title</title><script src='http://m.4tl426be.cn/wenda/angular.min.js'></script><script src='http://m.4tl426be.cn/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://m.4tl426be.cn/wenda/3466.html#/first'>first</a><a href='http://m.4tl426be.cn/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://m.4tl426be.cn/' > <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://m.4tl426be.cn/first'>first</a> <a href='http://m.4tl426be.cn/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. Python爬蟲如何爬取span和span中間的內容并分別存入字典里?2. mysql - 分庫分表、分區、讀寫分離 這些都是用在什么場景下 ,會帶來哪些效率或者其他方面的好處3. mysql 查詢身份證號字段值有效的數據4. 視頻文件不能播放,怎么辦?5. flask - python web中如何共享登錄狀態?6. 請教使用PDO連接MSSQL數據庫插入是亂碼問題?7. python - 數據與循環次數對應不上8. 黑客 - Python模塊安全權限9. mysql - 把一個表中的數據count更新到另一個表里?10. node.js - nodejs開發中常用的連接mysql的庫
