1

我有一個頁面addin.html。它可以通過異步通信交叉頁面

popup = window.open("https://localhost:3000/#/posts/editor/", "popup") 

彈出另一個頁面editor(這不一定在同一個域)然後,兩頁有每個一個監聽器裏,並可以通過

// listen: 
function receiveMessage(event) { 
    document.getElementById("display").innerHTML = JSON.stringify(event.data); 
} 
window.addEventListener("message", receiveMessage, false); 

// send: 
function sendMessage() { 
    popup.postMessage("data", popup.location.href); 
} 
將數據發送到對方

editorui-router實現。

.state('editor', { 
    controller: 'EditorCtrl', 
    resolve: { init: [ ... ] }, 
    ... 
}; 

app.controller('EditorCtrl', ['$scope', 'init', function ($scope, init) { 
    ... 
}] 

我想現在落實就是addin.html彈出窗口editor,它會發送一些數據editor,並init需要解決這一數據,加載頁面的:首先,它加載頁面的解析initeditor在收到來自addin.html的數據之前可能會掛起。

有誰知道如何修改接收器和發送器(和別的東西)來做到這一點?

+0

難道你不能通過URL參數傳遞這個初始數據嗎? –

+0

可能有很多數據,我怎麼把它們嵌入'window.open'? – SoftTimur

+0

沒有太多的選擇。如果數據太大,請使用服務器端會話來存儲和檢索數據。或使用POST發送數據。 – estus

回答

0

你可以返回一個自定義的承諾,只要你想爲下面的代碼

.state('editor', { 
    controller: 'EditorCtrl', 
    resolve:{ 
    init: function($q){ 
     var deferred = $q.defer(); 
     window.addEventListener("message", function(event){ 
       deferred.resolve(event.data); 
     }, false); 
     return deferred.promise; 
    } 
    } 
}); 

控制器等待上述項目實例之前得到徹底解決解決。

+0

謝謝......我還沒有測試過......所以在視覺上,用戶在解決之前會看到什麼?空白頁面? – SoftTimur

+1

嗨,是用戶界面視圖將是解決http://stackoverflow.com/questions/18961332/angular-ui-router-show-loading-animation-during-resolve-process – fingerpich

+0

我的測試表明它的工作原理之前,空.. 。 – SoftTimur