2015-01-14 15 views
8

私は2つの異なる角度のアプリを持っています。私は 'app_1'のiframeを使って 'app_2'のビューを開く必要があります。 また、「app_1」から「app_2」にデータを掲載する必要があります。 angularJSでこれを達成するには?2つのアングルアプリ間でiframeを使用して通信するにはどうすればよいですか?

ありがとうございます。 #SOS私はこれが1つのアプリがメッセージを送信することを意味するであろうと、他の1がメッセージに耳を傾けられる角度の面ではpostMessage ...

を使用することを検討して使用することになり

+0

なぜ:あなたのコードは次のようになります親のiFrameで

/** * App that sits within iframe: * * Inject this factory into your controller and call * iFrame.messageParentIframe({hello: 'world'}) to send messages * to the parent iFrame */ angular .module('app') .factory('iFrameMessenger', function($window) { return { messageParentIframe: function(message) { $window.parent.postMessage(message, '*'); } }; }); 

をこの2つのアプリを2つの異なるiframeでレンダリングしていますか?あなたはこれを行うことで何を達成しようとしていますか? –

答えて

14

だから、IFRAME内に位置アプリのあなたは、次の処理を行い、工場作ることができます。

/** 
* App that is on parent iframe: 
* 
* Just using a controller for the sake of simplicity, 
* but you could also use a Factory that only receives 
* messages from iFrames and handle them according to each 
* action, etc. You can get creative here on how you want 
* to handle it. 
*/ 
angular 
    .module('app') 
    .controller('AppCtrl', function($window) { 
    $window.addEventListener('message', function() { 
     // handle messages received from iframe 
    }); 
    }); 
+3

私はそのようなものが欲しかった...ありがとう@sergiocruz :) –

+0

すばらしい例。私はremoveEventListener()AppCtrlの$破棄イベントからだけ呼び出す必要があると思う何もが漏れていることを確認します。 – Damiox

+0

もう片方を離して(親は子iFrameにメッセージを送信します)、その2つの間のディスカッションの完全な例がありますか? – Ksyqo

関連する問題