angular.js - angularjs中路由的HTML5模式下的URL問題
問題描述
在angularjs的路由中使用HTML5模式,結(jié)果無法加載模版(templateUrl沒有發(fā)送http請求),而且URL中還出現(xiàn)的轉(zhuǎn)義。但使用標(biāo)簽?zāi)J綍r(shí)一切OK,求大佬告知?<!DOCTYPE html><html lang='en'><head>
<meta charset='UTF-8'><base href='http://www.aoyou183.cn/' ><title>Title</title><script src='http://www.aoyou183.cn/wenda/angular.min.js'></script><script src='http://www.aoyou183.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://www.aoyou183.cn/wenda/3466.html#/first'>first</a><a href='http://www.aoyou183.cn/wenda/3466.html#/second'>second</a>
</p><p ng-view></p></body></html>
問題解答
回答1:目錄結(jié)構(gòu)
模板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> <!--后面需根據(jù)部署后的實(shí)際路徑做調(diào)整--> <base href='http://www.aoyou183.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://www.aoyou183.cn/first'>first</a> <a href='http://www.aoyou183.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里的#號
相關(guān)文章:
