某些电子邮件客户端(显然)将网址中的哈希编码为%23,(例如#/ path#sectionId变为#/ path%23sectionId或%23 / path%23sectionId)。
访问此类网址时,使用$ routeProvider的其他优先级,并将您重定向到默认页面。 (我认为)
如何在$ routeProvider.when相应重定向之前解码网址?你知道吗?有什么解决办法吗?理想情况下,除了不使用哈希滚动到页面上的部分。
感谢。
答案 0 :(得分:0)
到目前为止我找到了一个解决方法:向我的主控制器添加$ location依赖关系,显示$ location.hash返回''和$ location.path返回'/ path#sectionId'。
基于此,我们可以在控制器的顶部添加如下内容:
// when has in url is encoded to %23, location.hash does not recognize it, and it gets cleared from the path in $RouteProvider
var holdHash = $location.path();
holdHash = holdHash.split('#');
if (holdHash.length > 1) { // if it has an item after the hash in the array
$location.hash(holdHash.pop()); //take that item and use it as a hash
}
但是你丢失了$ location.search中的值
或者你可以这样做:
$location.url(decodeURIComponent($location.url()))
这也保留了你的搜索,但取代'?'使用'%3F'然后将其返回$ location.ash而不是$ location.search。
他们在某种程度上是一个hacky解决方案,并且没有完全发挥作用(请参阅$ location.search的问题)
*****编辑2解决方案****
作为我问题的最终解决方案,我已经更新了存储id的服务,并在我的主控制器中首先触发哈希检测。还更新了has检测,以记住可以存储的所有变体:
/**
* tries to get hash from $location
* if unavailable tries to look for encoded hash
* if unavailable returns null
* @return {string or null} [a string with the id of the section or null]
*/
function getHashID() {
var fromLoc = $location.hash(),
fromEncodedUrl,
inPath;
if (fromLoc.length) {
return fromLoc;
}
// iphone Chrome encodes the pound sign on redirect
fromEncodedUrl = $location.url();
fromEncodedUrl = fromEncodedUrl.split('%23');
if (fromEncodedUrl.length > 1) {
return fromEncodedUrl.pop();
}
inPath = $location.path();
inPath = inPath.split('#');
if (inPath.length > 1) {
return inPath.pop();
}
return null;
}
我稍后使用存储在服务对象中的值触发动画,或获取新值,并在动画完成后清除服务中的值。