Angularjs iframe和其父对象之间的window.postMessage with Angular:有人有工作示例吗?
有没有人有一个在angular中如何发送和接收window.postMessage()调用的工作示例?我在github和ngmodules上找到了这个模块,但是我查看了这些代码,它对我来说没有多大意义,而且文档中缺少一个工作示例 编辑:添加失败的尝试 景色Angularjs iframe和其父对象之间的window.postMessage with Angular:有人有工作示例吗?,angularjs,postmessage,Angularjs,Postmessage,有没有人有一个在angular中如何发送和接收window.postMessage()调用的工作示例?我在github和ngmodules上找到了这个模块,但是我查看了这些代码,它对我来说没有多大意义,而且文档中缺少一个工作示例 编辑:添加失败的尝试 景色 <div simulation-host element="thing in things"></div> </div> <div id="debugConsole"> <h1&g
<div simulation-host element="thing in things"></div>
</div>
<div id="debugConsole">
<h1>MESSAGE CONSOLE</h1>
<p id="debugText"></p>
</div>
我试图发出指令
var simulationFrameHost = angular.module('proxy.directives', []);
simulationFrameHost.config(function ($sceDelegateProvider){
//URL Regex provided by Microsoft Patterns & Practices.
$sceDelegateProvider.resourceUrlWhitelist(['^(ht|f)tp(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&%\$#_]*)?$','self']);
});
simulationFrameHost.directive('simulationHost', ['$window',function($window) {
return {
retrict: 'ACE',
transclude: 'element',
replace: true,
scope: true,
template: [
'<ul>',
'<li>',
'<span>',
'<a href="#">',
'{{thing.location}}',
'</a>',
'</span>',
'<messenger>',
'<iframe ng-src="{{thing.resource}}" />',
'</messenger>',
'</li>',
'</ul>'
].join(''),
compile: function (tElement, tAttrs, transclude) {
var interval;
var show = function(msg)
{
var debugText = document.getElementById("debugText");
if(debugText){
debugText.innerHTML += msg + "<br/>";
}
};
var rpt = document.createAttribute('ng-repeat');
rpt.value = tAttrs.element;
console.log(tAttrs.element);
tElement[0].children[0].attributes.setNamedItem(rpt);
$(tElement[0].children[0].children[0].children[0]).on("click", function(event){
console.log(event);
var iframe = tElement[0].children[0].children[1].children[0].contentWindow;
show("Initiating connection with: " + event.currentTarget.host);
var message = {
action: 'syn',
param: 'connection'
};
interval = setInterval(function(){
//*****************************************
iframe.postMessage(JSON.stringify(message), 'http://'+ event.currentTarget.host);
//*****************************************
}, 500);
return false;
});
}
}
}]);
在我的指令的编译中,我试图将一个click事件连接到生成的锚标记。我正在尝试单击以将消息发布到iframe,但iframe.postMessage不起任何作用。它只是进入了虚空,我从今天早上10点开始就一直在做这个。我的眼睛开始变得呆滞:p
编辑:为单独容器之间的通用消息传递指令添加扩展要求(现在我有了功能代码),而不考虑容器类型:
1)i帧到父帧
2)窗口到窗口(我不能用角度指令来完成这项工作。我浪费了一整晚,试图“以正确的方式”完成这项工作我希望我能早点放弃这个想法,因为我的需求并没有真正保证。这个东西不需要扩展,因为它是专门构建的软件,用于在X域系统之间提供消息代理
'use strict'
var app = angular.module('domain.system.controllers', ['services.proxy.mine']);
app.controller('ProxyCtrl', ['$scope', '$routeParams', '$window', '$sce', 'MyService',
function ( $scope, $routeParams, $window, $sce, MyService)
{
$($window).on("message", function(e){
var message = JSON.parse(e.originalEvent.data);
if(message.recipient){
switch(message.recipient){
case: "ProxyCtrl":
//handle message;
break;
}
}
}
}
]);
我对如何将此代码转换为功能指令的详细说明100%感兴趣。刚刚看到这篇文章,这可能会对您有所帮助
(function() {
'use strict';
angular
.module('postmessage')
.factory('iFrameMessagingService', iFrameMessagingService);
iFrameMessagingService.$inject = ['$window', '$location', '$rootScope', '$log'];
function iFrameMessagingService($window, $location, $rootScope, $log) {
var service = {
sendMessage : sendMessage
};
activate();
return service;
function activate(){
activateIncomingMessageHandler();
}
function activateIncomingMessageHandler(){
$window.addEventListener('message', function(event){
if (typeof(event.data) !== 'undefined'){
// handle message
}
});
}
function sendMessage(message){
// Dispatch the event.
$log.debug(message);
if($window.parent !== $window){
$window.parent.postMessage(message, '*');
}
}
}
})();
您可以发布您尝试过的代码的示例,或者您尝试在angular中模拟的等效纯js代码吗?重构我当前的代码以仅隔离对postMessage的尝试是很困难的。我可以尝试。我作弊了。$(window).on(“message”,function(){});在控制器中。我花了太多的时间试图让它以“角度方式”工作。花了大约10分钟让它以“实际方式”工作如果我有时间,我会重构。实际可行。继续吧,并将其作为你的答案发布。+1这个!我自己刚刚花了大约3个小时尝试angular指令,然后不情愿地回到jQuery。同样,如果有人能告诉我们我们缺少了什么,我也会感兴趣。我很久以前写过这个问题。我没有必要修改它是这样的,但我在指令方面已经做得更好了。下次我在angular客户端工作时,我将尝试花几分钟时间看看是否可以组合一些东西来帮助您。您可以用angular.element($window)替换$($window)
?不要忘记在(“$destroy”,…)上删除$scope.$on中的侦听器。
$('a').on("click",function(event){
console.log(event);
var pop = window.open(event.currentTarget.href, 'poop');
show("Initiating connection with: " + event.currentTarget.host);
var message = {
action: 'syn',
param: 'connection',
};
interval = setInterval(function(){
pop.postMessage(JSON.stringify(message), 'http://'+ event.currentTarget.host);
}, 500);
return false;
});
$(window).on("message", function(e) {
clearInterval(interval);
var eventData = JSON.parse(e.originalEvent.data);
show("Message received from: " + e.originalEvent.origin);
if(eventData.action) {
switch (eventData.action) {
case 'syn-ack':
ack(e.originalEvent, eventData.param, eventData.value);
break;
case "set":
show("Set request received: " + e.originalEvent.data);
set(eventData.param, eventData.value);
break;
case "get":
show("Get request received: " + e.originalEvent.data);
var value = get(eventData.param);
var response = {
action: "response",
type: "get",
param: eventData.param,
value: value
};
e.originalEvent.source.postMessage(JSON.stringify(response), channel);
break;
}
}
});
'use strict'
var app = angular.module('domain.system.controllers', ['services.proxy.mine']);
app.controller('ProxyCtrl', ['$scope', '$routeParams', '$window', '$sce', 'MyService',
function ( $scope, $routeParams, $window, $sce, MyService)
{
$($window).on("message", function(e){
var message = JSON.parse(e.originalEvent.data);
if(message.recipient){
switch(message.recipient){
case: "ProxyCtrl":
//handle message;
break;
}
}
}
}
]);
(function() {
'use strict';
angular
.module('postmessage')
.factory('iFrameMessagingService', iFrameMessagingService);
iFrameMessagingService.$inject = ['$window', '$location', '$rootScope', '$log'];
function iFrameMessagingService($window, $location, $rootScope, $log) {
var service = {
sendMessage : sendMessage
};
activate();
return service;
function activate(){
activateIncomingMessageHandler();
}
function activateIncomingMessageHandler(){
$window.addEventListener('message', function(event){
if (typeof(event.data) !== 'undefined'){
// handle message
}
});
}
function sendMessage(message){
// Dispatch the event.
$log.debug(message);
if($window.parent !== $window){
$window.parent.postMessage(message, '*');
}
}
}
})();