2015-01-14 6 views
8

मेरे पास दो अलग-अलग कोणीय ऐप्स हैं। मुझे 'app_1' में iframe का उपयोग करके 'app_2' का दृश्य खोलना होगा। मुझे 'app_1' से 'app_2' तक कुछ डेटा पोस्ट करने की भी आवश्यकता है। angularJS में इसे कैसे प्राप्त करें?2 कोणीय ऐप्स के बीच iframe का उपयोग करके संवाद कैसे करें?

अग्रिम धन्यवाद। #SOS

+0

तुम क्यों इन दो ऐप को दो अलग-अलग आईफ्रेम में प्रस्तुत कर रहे हैं? ऐसा करने से आप क्या हासिल करने जा रहे हैं? –

उत्तर

14

मैं postMessage उपयोग करने पर विचार का प्रयोग करेंगे ...

कोणीय संदर्भ में इसका मतलब यह होगा कि एक अनुप्रयोग संदेश और अन्य एक संदेशों को सुनने की जाएगी भेजना होगा।

अनुप्रयोग है कि 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, '*'); 
     } 
    }; 
    }); 

माता पिता 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

शानदार उदाहरण। मुझे लगता है कि आपको यह सुनिश्चित करने के लिए कि कुछ भी लीक नहीं हुआ है, आपको AppCtrl में $ विनाश घटना से removeEventListener() को कॉल करना चाहिए। – Damiox

+0

दूसरे को दूर करने के बारे में क्या (माता-पिता को बच्चे iFrame को संदेश भेजना), तो हमारे पास दोनों के बीच चर्चा का एक पूरा उदाहरण है? – Ksyqo

संबंधित मुद्दे