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. sublime text3安裝package control失敗2. wordpress里,這樣的目錄列表是屬于小工具還是啥?3. python如何設置一個隨著系統時間變化的動態變量?4. 常量在外面不加引號會報錯。5. 一直報這個錯誤6. mysql federated引擎無法開啟7. mysql - 大部分數據沒有行溢出的text字段是否需要拆表8. MySQL 使用 group by 之后然后 IFNULL(COUNT(*),0) 為什么還是會獲得 null9. mysql 為什么主鍵 id 和 pid 都市索引, id > 10 走索引 time > 10 不走索引?10. 我的怎么不顯示啊,話說有沒有QQ群什么的
