angularjs路由path方式实现原理探究

angularjs路由

https://angular.io/guide/router

通过URL解释, 来定位客户端生成的浏览器端视图。

你可绑定路由到页面的链接上, 当用户点击链接, 可以浏览到相应的应用视图。

The browser is a familiar model of application navigation:

  • Enter a URL in the address bar and the browser navigates to a corresponding page.
  • Click links on the page and the browser navigates to a new page.
  • Click the browser‘s back and forward buttons and the browser navigates backward and forward through the history of pages you‘ve seen.

The Angular Router ("the router") borrows from this model. It can interpret a browser URL as an instruction to navigate to a client-generated view. It can pass optional parameters along to the supporting view component that help it decide what specific content to present. You can bind the router to links on a page and it will navigate to the appropriate application view when the user clicks a link.

特点:URL改变没有发起http请求

路由方法有三种:

1、 通过url path

2、 通过hash

3、通过query string

按照http协议,对于地址栏的一个url访问, 总会发起http请求, 刷新页面, 获得页面视图, 这个在后台MVC框架中, 是正常的事情。

但是我们观察, 在angularjs框架的路由效果中, url改变,并没有刷新页面, 这让我身为好奇, 决定一查究竟。

资料搜集

https://stackoverflow.com/questions/3338642/updating-address-bar-with-new-url-without-hash-or-reloading-the-page

Updating address bar with new URL without hash or reloading the page


662 down vote accepted

You can now do this in most "modern" browsers!

Here is the original article I read (posted July 10, 2010): HTML5: Changing the browser-URL without refreshing page.

For a more in-depth look into pushState/replaceState/popstate (aka the HTML5 History API) see the MDN docs.

TL;DR, you can do this:

window.history.pushState("object or string", "Title", "/new-url");

See my answer to Modify the URL without reloading the page for a basic how-to.

https://stackoverflow.com/questions/824349/modify-the-url-without-reloading-the-page

This can now be done in Chrome, Safari, FF4+, and IE10pp4+!

See this question‘s answer for more info: Updating address bar with new URL without hash or reloading the page

Example:

 function processAjaxData(response, urlPath){
     document.getElementById("content").innerHTML = response.html;
     document.title = response.pageTitle;
     window.history.pushState({"html":response.html,"pageTitle":response.pageTitle},"", urlPath);
 }

You can then use window.onpopstate to detect the back/forward button navigation:

window.onpopstate = function(e){
    if(e.state){
        document.getElementById("content").innerHTML = e.state.html;
        document.title = e.state.pageTitle;
    }
};


For a more in-depth look at manipulating browser history see this MDN article.

How can I change the page URL without refreshing the page?


27 down vote accepted

In HTML5 you can change the URL:

window.history.pushState("object or string", "Title", "/new-url");

check http://spoiledmilk.com/blog/html5-changing-the-browser-url-without-refreshing-page/

docs: https://developer.mozilla.org/en/DOM/Manipulating_the_browser_history#The_pushState().c2.a0method



UPDATE

An overview of which browser support the new HTML5 history API:

http://caniuse.com/#search=pushState (caniuse.com is worth to bookmark!)

there are already frameworks that do the hard work for you and even gracefully fallback to the common hash-tag solution:

总结:归功于HTML5的 BOM history对象

https://developer.mozilla.org/en-US/docs/Web/API/History_API#The_pushState().c2.a0method

Example of pushState() method

Suppose http://mozilla.org/foo.html executes the following JavaScript:

var stateObj = { foo: "bar" };
history.pushState(stateObj, "page 2", "bar.html");

This will cause the URL bar to display http://mozilla.org/bar.html, but won‘t cause the browser to load bar.html or even check that bar.html exists.

Suppose now that the user now navigates to http://google.com, then clicks back. At this point, the URL bar will display http://mozilla.org/bar.html, and the page will get a popstate event whose state object contains a copy of stateObj. The page itself will look like foo.html, although the page might modify its contents during the popstate event.

If we click back again, the URL will change to http://mozilla.org/foo.html, and the document will get another popstate event, this time with a null state object. Here too, going back doesn‘t change the document‘s contents from what they were in the previous step, although the document might update its contents manually upon receiving the popstate event.

https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onpopstate

window.onpopstate = function(event) {
  alert("location: " + document.location + ", state: " + JSON.stringify(event.state));
};

history.pushState({page: 1}, "title 1", "?page=1");
history.pushState({page: 2}, "title 2", "?page=2");
history.replaceState({page: 3}, "title 3", "?page=3");
history.back(); // alerts "location: http://example.com/example.html?page=1, state: {"page":1}"
history.back(); // alerts "location: http://example.com/example.html, state: null
history.go(2);  // alerts "location: http://example.com/example.html?page=3, state: {"page":3}

window.onpopstate = function(event) {
  alert("location: " + document.location + ", state: " + JSON.stringify(event.state));
};

history.pushState({page: 1}, "title 1", "?page=1");
history.pushState({page: 2}, "title 2", "?page=2");
history.replaceState({page: 3}, "title 3", "?page=3");
history.back(); // alerts "location: http://example.com/example.html?page=1, state: {"page":1}"
history.back(); // alerts "location: http://example.com/example.html, state: null
history.go(2);  // alerts "location: http://example.com/example.html?page=3, state: {"page":3}

时间: 2024-08-06 03:42:03

angularjs路由path方式实现原理探究的相关文章

《coredump问题原理探究》Windows版 笔记

<coredump问题原理探究>Windows版 笔记 Debug 一.环境搭建 1.Win7捕获程序dump 2.Windbg符号表设置(Symbols Search Path) 二.WinDbg命令 三.函数栈帧 1.栈内存布局 2.栈溢出 3.栈的规律 4.定位栈溢出问题的经验方法 四.函数逆向 五.C内存布局 1.基本类型 2.数组类型 3.结构体 六.C++内存布局 1.类的内存布局 2.this指针 3.虚函数表及虚表指针 4.单继承 5.多继承(无公共基类) 七.STL容器内存布

Angularjs路由需要了解的那点事

我们知道angularjs是特别适合单页面应用,为了通过单页面完成复杂的业务功能,势必需要能够从一个视图跳转到另外一个视图,也就是需要在单个页面里边加载不同的模板.为了完成这个功能angularjs为我们提供了路由服务($routeProvider). 先看下我们的示例代码,html框架页index.html <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&qu

AngularJS 路由

AngularJS 路由允许我们通过不同的 URL 访问不同的内容. 通过 AngularJS 可以实现多视图的单页Web应用(single page web application,SPA). 通常我们的URL形式为 http://runoob.com/first/page,但在单页Web应用中 AngularJS 通过 # + 标记 实现,例如: http://runoob.com/#/first http://runoob.com/#/second http://runoob.com/#/

AngularJS“多重路由”嵌套模块——AngularJS“路由”嵌套学习资料教程

这是小编的一些学习资料,理论上只是为了自己以后学习需要的,但是还是需要认真对待的 以下内容仅供参考,请慎重使用学习 1.AngularJS路由嵌套 Angularjs本身自带路由模块,可以满足通过不同的 URL 访问不同的内容,当然实际应用为在单页面点击不同按钮等加载不同页面 之前有关于angular-route路由的介绍,但是只能一层路由嵌套,如果需要多重嵌套就是不够用了 UI-Router作为AngularUI为开发者提供的其中实用的一个模块,根据URL状态组织和控制界面UI的渲染,不是仅仅

路由及路由器工作原理深入解析2:路由原理

日志"路由及路由器工作原理深入解析1"http://user.qzone.qq.com/2756567163/blog/1438322342介绍了"为什么要使用路由器"和"TCP/IP V4 协议网络的分段原理"2个问题,本文将继续对路由的具体工作原理进行解析. 3.路由原理 当IP子网中的一台主机发送IP分组给同一IP子网的另一台主机时,它将直接把IP分组送到网络上,对方就能收到.而要送给不同IP子网上的主机时,它要选择一个能到达目的子网上的路

zookeeper使用和原理探究(一)(转)

zookeeper介绍zookeeper是一个为分布式应用提供一致性服务的软件,它是开源的Hadoop项目中的一个子项目,并且根据google发表的<The Chubby lock service for loosely-coupled distributed systems>论文来实现的,接下来我们首先来安装使用下这个软件,然后再来探索下其中比较重要一致性算法. zookeeper安装和使用zookeeper的安装基本上可以按照 http://hadoop.apache.org/zookee

vmware nat模式原理探究,实现虚拟机跨网段管理

vmware nat模式原理探究: 理解nat模式,我们能更加了解主机与虚拟机之间如何通信,以及虚拟机如何实现上网. 以及便于我们分析虚拟机与主机无法通信和无法上外网的问题. 下面通过实战:虚拟网络拓扑,抓包分析. 为什么要探究nat模式? 从日常需求出发: 我们电脑上用到的虚拟机越来越多,需要固定IP,方便管理. 网络环境经常会变,我们可能在家办公和在公司办公网段不一样,每次要改为同一个网段再连接使用,比较麻烦. 首先理解vmware的三种模式: bridge模式:相当于一台hub,真实主机与

再谈angularJS数据绑定机制及背后原理—angularJS常见问题总结

Angular 的数据绑定采用什么机制,详述原理? 脏检查机制.阐释脏检查机制,必须先了解如下问题. 单向绑定(ng-bind) 和 双向绑定(ng-model) 的区别? ng-bind 单向数据绑定($scope -> view),用于数据显示,简写形式是 {{}}. 两者的区别在于页面没有加载完毕 {{val}} 会直接显示到页面,直到 Angular 渲染该绑定数据(这种行为有可能将 {{val}} 让用户看到):而 ng-bind 则是在 Angular 渲染完毕后将数据显示. ng-

前端跨域问题各种解决方式及原理

跨域的各种解决方式及原理 因为浏览器有某些安全级别的限制,例如,同源策略,所以在进行浏览器端的web应用开发的时候,经常会遇到跨域问题. 同源策略:只有在同源的情况下(同域名,同协议,同端口)才能进行数据交互 跨域问题报错:XMLHttpRequest cannot load https://api.douban.com/v2/movie/in_theaters. No 'Access-Control-Allow-Origin' header is present on the requeste