angular.js - angularjs中路由的HTML5模式下的URL问题
问题描述
在angularjs的路由中使用HTML5模式,结果无法加载模版(templateUrl没有发送http请求),而且URL中还出现的转义。但使用标签模式时一切OK,求大佬告知?<!DOCTYPE html><html lang='en'><head>
<meta charset='UTF-8'><base href='https://www.haobala.com/' ><title>Title</title><script src='https://www.haobala.com/wenda/angular.min.js'></script><script src='https://www.haobala.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='https://www.haobala.com/wenda/3466.html#/first'>first</a><a href='https://www.haobala.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='https://www.haobala.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='https://www.haobala.com/first'>first</a> <a href='https://www.haobala.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. 微信订阅号如何用渠道二维码统计地推效果?2. django - 后台返回的json数据经过Base64加密,获取时用python如何解密~!3. mysql - PHP 批量插入的时候 判断数据表里面是否重复怎么优化4. 并发 - python多进程+协程的正确姿势?5. mysql - 数据库表结构设计6. 网页爬虫 - python爬虫翻页问题,请问各位大神我这段代码怎样翻页,还有价格要登陆后才能看到,应该怎么解决7. javascript - QQ第三方登录的问题8. MySQL 水平拆分之后,自动增长的ID有什么好的解决办法?9. python sqlalchemy create_engine问题10. mysql如何多字段去重呢?
![android - MVP模式 包的结构](http://www.haobala.com/attached/image/news/202311/095852d398.png)