好的我花了一點時間爲你提出一個工作解決方案。但是它預設了一定程度的一致性和對角度應用程序的控制。
步驟之一就是簡單的鏈接導航添加到我的子角applciation的部分:
<nav>
<ul>
<li><a href="#/app1/main">App1</a></li>
<li><a href="#/app2/home">App2</a></li>
</ul>
</nav>
同父頁我還添加了一個iframe,你需要 :)
<section>
<iframe src=""></iframe>
</section>
用以下腳本來處理父窗口中的地址更改:
// Simple method to look for the second index of something
String.prototype.secondIndexOf = function (val) {
var fst = this.indexOf(val);
var snd = this.indexOf(val, fst + 1)
return snd
}
// My goto method to basically go to somewhere in my angular apps
function goto(app, route) {
$('iframe').attr('src', app + '/test.html#' + route)
}
// upon hash change of the parent page I will call my goto method if there is a hash
$(window).on('hashchange', function() {
var hash = location.hash;
if (hash) {
var appName = hash.substring(hash.indexOf('app'), hash.indexOf('app') + 4);
var route = hash.substring(hash.secondIndexOf('/'));
goto(appName, route);
}
});
// On initial page load I also like to trigger this hash change to
// go to the right location initially
$(window).trigger('hashchange');
顯然,這似乎是一種單向解決方案,除非我們還創建了從子角度應用程序向後修改父窗口的向後url。
爲了實現這一目標,我決定推哈希值發生變化回升到每個應用程序的$ routeChangeStart事件中的父窗口:
因此,例如,對於APP1這將是:
.run(["$rootScope", function ($rootScope) {
$rootScope.$on('$routeChangeStart', function(next, current) {
if (current.$$route){
window.parent.location.hash = "#/app1" + current.$$route.originalPath;
}
});