angular : $location & $state(UI router)的关系

时间:2023-03-09 08:54:37

次序:angular 的 location会先跑

$rootScope.$on("$locationChangeStart", function (scope, newUrl, oldUrl) {
log("$locationChangeStart from " + oldUrl)
log("$locationChangeStart to " + newUrl)
});

接着是 ui router $state

$rootScope.$on("$stateChangeStart", function (event, toState, toParams, fromState, fromParams) {
log("$stateChangeStart from " + fromState.name)
log("$stateChangeStart to " + toState.name)
});

log出来的结果是$stateChangeStart 的fromState 是空,这是router UI 的机制,一开始没有state 去到state是会detect到

router UI $state是监听 angular 的$location,如果之前的url 或 state是一样的就不会触发。

$location.hash()是用digest换url的