angular 路由去除#号
1. 路由启动 $locationProvider.html5Mode(true); 通过pushstatex修改url
app.js
define([
'angular',
"App/Ctrl/controllers",
"App/Directive/directive", "angularRoute"
], function (angular,controllers,directives,appDirec) {
var app=angular.module('myApp', ["ngRoute",controllers.name,directives.name])
templete="/front/propertyEntrust/view/templete"
/* /limitSell/add?propertyId=33 */ app.config(['$routeProvider',"$locationProvider", function ($routeProvider,$locationProvider) { $locationProvider.html5Mode(true); $routeProvider.when('/detail/:Id', { //详情页面
templateUrl: templete+'/detail.html'
}); $routeProvider.when('/rent/add/:propertyId', { //一般出租
templateUrl: templete+'/rent.html'
}); $routeProvider.otherwise({redirectTo: '/rent'});
}]); return app
});
2. 设置前端路由开始的字段 即服务器路由的最后的字段
<base href="/fy/propertyEntrustApply/index/">
小注:angular在此处使用html5的base标签来做baseurl的配置,而不是提供API 配置baseurl 非常巧妙,充分利用了html5 base标签的特性。 debug 所有$0.href 发现都自动加上了了“/client.app/index”.
这样在模板 或者.routeprovider 配置路由的时候就不需要在另外拼装上baseurl了。节省了很多对url逻辑的处理。 但是 如果是script加载脚本 相对路径的话 可能会与base的配置冲突,我的项目使用requirejs一类的包加载器解决问题
3,后端配置重定向 nodejs为例
app.get('/fy/propertyEntrustApply/index/*', function (req, res) {
res.render("a", {});
});
如上所示 http://localhost:3000/fy/propertyEntrustApply/index/rent/add/21
/fy/propertyEntrustApply/index/ 为服务器路由 指向a.ejs
之后/rent/add/21 就是前端路由了
源码小解:
对路由监听路由其实是监听rootscope, loaction.path()的修改会自动更新rootscope,所以对location.path()的作用其实类似backbone的navigate(), 逻辑是手动走了路由随后更改url的值,而不是相反。
其次 angular对视图内的所有超链接做了事件代理
$rootElement.on('click', function(event) {
// TODO(vojta): rewrite link when opening in new tab/window (in legacy browser)
// currently we open nice url link and redirect then
if (event.ctrlKey || event.metaKey || event.which == 2) return;
var elm = jqLite(event.target);
// traverse the DOM up to find first A tag
while (lowercase(elm[0].nodeName) !== 'a') {
// ignore rewriting if no A tag (reached root element, or no parent - removed from document)
if (elm[0] === $rootElement[0] || !(elm = elm.parent())[0]) return;
}
var absHref = elm.prop('href');
if (isObject(absHref) && absHref.toString() === '[object SVGAnimatedString]') {
// SVGAnimatedString.animVal should be identical to SVGAnimatedString.baseVal, unless during
// an animation.
absHref = urlResolve(absHref.animVal).href;
}
var rewrittenUrl = $location.$$rewrite(absHref);
if (absHref && !elm.attr('target') && rewrittenUrl && !event.isDefaultPrevented()) {
event.preventDefault();
if (rewrittenUrl != $browser.url()) {
// update location manually
$location.$$parse(rewrittenUrl);
$rootScope.$apply();
// hack to work around FF6 bug 684208 when scenario runner clicks on links
window.angular['ff-684208-preventDefault'] = true;
}
}
});
可以看到, 点击超链接的默认行为被禁止,如果超链接是负责路由跳转的时候,获得路由的url, 手动执行$rootScope.$apply();
注意$().prop 与$().attr()的区别 主要在$().attr() 拿的是$0.href, $().prop拿的是$0.getAttribute("href") , 此处需要去除 base路径的拼装,只拿前端路由。所以使用$().prop
$rootScope.$watch(function $locationWatch() {
var oldUrl = $browser.url();
var currentReplace = $location.$$replace;
if (!changeCounter || oldUrl != $location.absUrl()) {
changeCounter++;
$rootScope.$evalAsync(function() {
if ($rootScope.$broadcast('$locationChangeStart', $location.absUrl(), oldUrl).
defaultPrevented) {
$location.$$parse(oldUrl);
} else {
$browser.url($location.absUrl(), currentReplace);
afterLocationChange(oldUrl);
}
});
}
$location.$$replace = false;
return changeCounter;
});
angular 会watch rootscope,当rootscope更新的时候 $browser.url() H5模式启用的条件下 调用pushstate 修改浏览器 url的值,最后afterLocationChange(oldUrl),广播 $locationChangeSuccess 事件。
然后在 angular-rout.js ngroute模块中会监听$locationChangeSuccess 事件,执行路由模块的逻辑。
$rootScope.$on('$locationChangeSuccess', updateRoute);
angular 路由去除#号的更多相关文章
- angular路由——ui.route
angular路由 使用案例 <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...
- angular路由详解:
1.$routeProvider ngRoute模块中的服务 2.otherwise:设置用于路由改变时,与任何其他定义的路由无法匹配的时候执行的代码 3.when:为$route服务定义新的路由 例 ...
- angular 路由的引用
使用angular路由 遇到的坑. 使用cmd 安装好node.js 安装成功 输入node -v 能查看版本说明安装成功 搭建angular项目输入命令 npm install -g angu ...
- angular 路由项目例子
angular 路由是我在工作中体验非常便捷的一点, 这是详细的API ,查看API 可以了解很多东西, https://github.com/angular-ui/ui-router/wiki/Qu ...
- Angular路由参数传递
一.路由时传递参数的方式 1.在查询参数中传递数据 //页面 <a routerLink="/product" [queryParams]="{id:1}" ...
- angular路由(自带路由篇)
一.angular路由是什么? 为了实现SPA多视图的切换的效果,其原理可简述为每个 URL 都有对应的视图和控制器.所以当我们给url后面拼上不同的参数就能通过路由实现不同视图的切换. 二.文件总览 ...
- Angular路由守卫 canActivate
作用 canActivate 控制是否允许进入路由. canActivateChild 等同 canActivate,只不过针对是所有子路由. 关键代码 创建路由守卫 import { Injecta ...
- Angular路由守卫 canDeactivate
目的 离开页面时,做出逻辑判断 以ng-alain的项目为基础做演示 效果如图: 关键代码 定义一个CanDeactivateGuardService export class CanDeactiva ...
- Angular 路由守卫
1. 路由 Angular路由: 可以控制页面跳转:可以在多视图间切换: 2. 路由守卫 Angular路由守卫: 在进入或离开某路由时,用于判断是否可以离开.进入某路由::: return true ...
随机推荐
- C#中==与Equals的区别
1.字符串: string s1 = "test"; string s2 = "test"; , ); object s4 = s3; Console.Writ ...
- ural 2073. Log Files
2073. Log Files Time limit: 1.0 secondMemory limit: 64 MB Nikolay has decided to become the best pro ...
- 【BZOJ2049】 [Sdoi2008]Cave 洞穴勘测 LCT/并查集
两种方法: 1.LCT 第一次LCT,只有link-cut和询问,无限T,到COGS上找了数据,发现splay里的父亲特判出错了(MD纸张),A了,好奇的删了反转T了.... #include < ...
- man/info
提示符方面,在linux当中,默认root的提示符为#,而一般身份用户的提示字符为$. 1.重新启动X Window 的快速按钮 一般来说,我们是可以手动来直接修改X Window 的配置文件的,不过 ...
- Java生成CSV文件实例详解
本文实例主要讲述了Java生成CSV文件的方法,具体实现步骤如下: 1.新建CSVUtils.java文件: package com.saicfc.pmpf.internal.manage.utils ...
- How to get SQLite work on windows phone 8
1.Install SQLite for Windows Phone SDKC:\Program Files (x86)\Microsoft SDKs\Windows Phone\v8.0\Exten ...
- js从身份证号中获取出生日期和性别
今天,在做移动端的项目中,按照设计稿的要求,是可以让用户自己输入出生日期的,我还很认真的用了刚刚知道的html5表单的日期类型,本想着终于不用日期插件就可以实现用户选择自己的出生日期了,可结果老大说, ...
- Hibernate---单条记录的增删改查
package com.hanqi.test; import static org.junit.Assert.*; import java.util.Date; import org.hibernat ...
- LabVIEW如何将脚本插入Quick Drop
问题:如何将自己设计的LabVIEW脚本做成快捷键的方式,实现效果如下 解决: 第一步:在LabVIEW Data中新建Quick Drop Plugins 第二步 在文件夹下新建一个VI,VI接口的 ...
- acm常见算法及例题
转自:http://blog.csdn.net/hengjie2009/article/details/7540135 acm常见算法及例题 初期:一.基本算法: (1)枚举. (poj17 ...