大致上了解一下有什么功能操作,之后打算自己实现这个.
controller("ctrl", ["$scope", "$location", function ($scope, $location) { //hash pattern 是 originUrl + "#/path?paraKey" //$location 是同步当前url的,所以你set paraKey 等都会改变 url //start url : http://localhost:5715/Angularjs/location/Default.aspx var location_obj = $location.url("/path?x=y y"); //添加 hash, #/path?x=y 到url最后, 返回$location对象 //now url : http://localhost:5715/Angularjs/location/Default.aspx#/path?x=y%20y var afterHash_str = $location.url(); //返回hash#后面的str : /path?x=y%20y (加密) var currentUrl = $location.absUrl(); //location.href. (加密) var http = $location.protocol(); //location.protocol var host = $location.host(); //location.host var port = $location.port(); //location.port var path = $location.path(); //返回#之后的/path, without paraKey var hash = $location.hash(); //不会 var paraKeys = $location.search(); //返回paraKey对象,(解密) $location.search({ x: "z z 1", y: "abc" }); //add and overwrite paraKey , cant clear $location.search("y", null); //clear a paraKey }]);
更新一下 :
如果要获取 url 的 params
$location.search().paramKey
domain.com?paramKey=123 HTML5 mode 情况下
domain.com#/?paramKey=123 hash mode 情况下
更新 :
$locationChangeSuccess 会在2种情况下触发,
一是url改变的时候(通过 js 监听 onhashChange or onpopstate event)
二是当digest的时候 (angular 写了一个 watch(function (){..} ))
$locationChangeSuccess 会有一次的初始化运行,接着只有在 url not same 的情况下才会触发.
更新 : 2016-02-13
HTML5 mode :
需要 <base> refer : http://www.w3schools.com/tags/tag_base.asp
<head> <base href="https://localhost:44301/"> </head>
app.config setup
var app = angular.module("app", []); app.config(function ($locationProvider) { $locationProvider.html5Mode({ enabled: true, requireBase: true }); });
如果 <a> 不想使用angular route 而希望redirect的话, 加 attribute target="_self"
<a href="/ext/link?a=b" target="_self">link</a>