Angular 1.6版本路由中/#!/的解决方法
来源:互联网 发布:淘宝现货怎么抢最快 编辑:程序博客网 时间:2024/06/05 20:51
AngularJS 路由 是通过 # + 标记 帮助我们区分不同的逻辑页面并将不同的页面绑定到对应的控制器上。因此在设置好路由规则后,为html页面的a标签设置href路由链接切换不同的视图。Angular1.6版本之前通常有href=“#...”或href=“#/...”这两种写法,结果这两种写法在Angular1.6中没有任何反应。
结果查看了下浏览器地址栏,默认视图链接竟然显示“#!/..”,是的,中间多加了个!号。
AngularJS升级到了1.6版本后,里面多了很多/#!/
的改动。那么1.6究竟做了哪些改变呢?可以参考这个:https://github.com/angular/angular.js/commit/aa077e81129c740041438688dff2e8d20c3d7b52
解决方案一:
所以在html页面a标签上将href的属性值添加一个!号就可以了。
<p><a href="#!/addStudent">添加学生</a></p><p><a href="#!/viewStudents">查看学生</a></p>完整代码:
<html><head><meta charset="utf-8" /><title>AngularJS 视图</title><script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.min.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular-route.min.js"></script></head><body><h2>AngularJS 视图</h2><div ng-app="mainApp"><p><a href="#!/addStudent">添加学生</a></p><p><a href="#!/viewStudents">查看学生</a></p><div ng-view></div><script type="text/ng-template" id="addStudent.html"><h2>添加学生</h2>{{message}}</script><script type="text/ng-template" id="viewStudents.html"><h2>查看学生</h2>{{message}}</script></div><script>var mainApp=angular.module("mainApp",['ngRoute']);mainApp.config(["$routeProvider",function($routeProvider){$routeProvider.when('/addStudent',{templateUrl:'addStudent.html',controller:'AddStudentController'}).when('/viewStudents',{templateUrl:'viewStudents.html',controller:'ViewStudentsController'}).otherwise({redirectTo:'/addStudent'});}]);mainApp.controller("AddStudentController",function($scope){$scope.message="这个页面是用于显示学生信息的输入表单";});mainApp.controller("ViewStudentsController",function($scope){$scope.message="这个页面是用于显示所有学生信息";});</script></body></html>
解决方案二:
如果想让路由依旧表现的与之前版本的一致可以这样做:
mainApp.config(["$locationProvider","$routeProvider",function($locationProvider,$routeProvider){ $locationProvider.hashPrefix('');}]);
<p><a href="#addStudent">添加学生</a></p><p><a href="#viewStudents">查看学生</a></p>完整代码:
<html><head><meta charset="utf-8" /><title>AngularJS 视图</title><script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.min.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular-route.min.js"></script></head><body><h2>AngularJS 视图</h2><div ng-app="mainApp"><p><a href="#addStudent">添加学生</a></p><p><a href="#viewStudents">查看学生</a></p><div ng-view></div><script type="text/ng-template" id="addStudent.html"><h2>添加学生</h2>{{message}}</script><script type="text/ng-template" id="viewStudents.html"><h2>查看学生</h2>{{message}}</script></div><script>var mainApp=angular.module("mainApp",['ngRoute']);mainApp.config(["$locationProvider","$routeProvider",function($locationProvider,$routeProvider){ $locationProvider.hashPrefix('');$routeProvider.when('/addStudent',{templateUrl:'addStudent.html',controller:'AddStudentController'}).when('/viewStudents',{templateUrl:'viewStudents.html',controller:'ViewStudentsController'}).otherwise({redirectTo:'/addStudent'});}]);mainApp.controller("AddStudentController",function($scope){$scope.message="这个页面是用于显示学生信息的输入表单";});mainApp.controller("ViewStudentsController",function($scope){$scope.message="这个页面是用于显示所有学生信息";});</script></body></html>
这样浏览器访问时,就不会多出个!号了。
1 0
- Angular 1.6版本路由中/#!/的解决方法
- AngularJS1.6版本中ui-router路由中/#!/的解决方法
- angular路由的使用
- 简单的angular路由
- angular的路由实现,ngRoute
- angular路由的深入理解
- angular的路由表格添加
- angular路由的使用案例
- angular之路由的理解
- angular路由的简单应用
- Angular中嵌套路由ui-router
- node.js服务器中支持angular路由
- angular 路由
- angular路由
- Angular路由
- angular 路由
- Angular路由
- angular路由
- Oracle EBS Forms数字签名无法验证卡住解决方法
- 前端工程师常用可视化工具
- 关于echarts图表传json对象,树状图传参数的建议
- 慕课-程序设计与算法(大学先修课)-郭炜-第三周练习题
- poj 3579 二分加尺取法
- Angular 1.6版本路由中/#!/的解决方法
- ExpressBox 3450
- MySQL中的约束,添加约束,删除约束,以及一些其他修饰
- iOS delegate使用
- Java三大注释
- Ubuntu下安装ffmpeg完整教程
- Git使用之Windows环境下配置
- 高压互锁
- Redis Cluster集群的搭建与实践