Javascript $window.addEventListener未在中触发
当消息到达时,我试图刷新角度表,通知新数据已准备就绪;我尝试使用以下代码,提示如下: , 但在以下情况下,甚至未接收到事件:Javascript $window.addEventListener未在中触发,javascript,angularjs,Javascript,Angularjs,当消息到达时,我试图刷新角度表,通知新数据已准备就绪;我尝试使用以下代码,提示如下: , 但在以下情况下,甚至未接收到事件: parent.postMessage("printComments", "*"); 由帧中的另一页发送 有什么事吗 <div ng-controller="MyCtrl" ng-app="myapp"> <table ng-table="commentsTable"> <tr ng-repeat="
parent.postMessage("printComments", "*");
由帧中的另一页发送
有什么事吗
<div ng-controller="MyCtrl" ng-app="myapp">
<table ng-table="commentsTable">
<tr ng-repeat="item in obj.data track by $index">
<td class="plantCell">{{item.nome}}: </td>
<td class="statusCell">{{item.status}}</td>
<td class="statusCell">{{item.testo}}</td>
</tr>
</table>
</div>
<script language="javascript">
var app=angular.module('myapp', []);
var printOperation;
function GetFromLocalStorage(key) {
var items=localStorage.getItem(key);
console.log(items);
if (items===null){
console.log("item null");
return null;
} else {
if (typeof items!= "string") {items = JSON.stringify(items);}
return items;
}
}
app.service('servicename', function($window, $rootScope) {
function subsFunc() {
$window.addEventListener('printComments',
function(e) {
alert("arriva evento");
$rootScope.$broadcast('app.clickEvent', e);
});
}
return {
"subscribeMe": subsFunc
}
});
app.controller('MyCtrl',
function($scope,servicename) {
$scope.data = {};
$scope.printComments = function() {
//$scope.obj=GetFromLocalStorage("AllComments");
$scope.data.obj = [{
"nome": "first",
"status": 1,
"testo": "Rottura rullo 1!!!"
}, {
"nome": "second",
"status": 0,
"testo": "Rottura rullo fsdfsf!!!"
}];
console.log("ricevo evento e ricarico tabella");
console.log($scope.data.obj);
}
servicename.subscribeMe();
$scope.$on('app.clickEvent', function(a, b) {
console.log("evento");
alert("on received");
$scope.printComments();
});
}
)
{{item.nome}}:
{{item.status}
{{item.testo}
var-app=angular.module('myapp',[]);
var打印操作;
函数GetFromLocalStorage(键){
var items=localStorage.getItem(键);
控制台日志(项目);
如果(项===null){
console.log(“项空”);
返回null;
}否则{
if(typeof items!=“string”){items=JSON.stringify(items);}
退货项目;
}
}
app.service('servicename',function($window,$rootScope){
函数subfunc(){
$window.addEventListener('printComments',
职能(e){
警报(“到达事件”);
$rootScope.$broadcast('app.clickEvent',e);
});
}
返回{
“subscribeMe”:子函数
}
});
应用程序控制器('MyCtrl',
函数($scope,servicename){
$scope.data={};
$scope.printComments=函数(){
//$scope.obj=GetFromLocalStorage(“AllComments”);
$scope.data.obj=[{
“诺姆”:“第一”,
“地位”:1,
“testo”:“Rottura rullo 1!!!”
}, {
“nome”:“second”,
“状态”:0,
“testo”:“Rottura rullo fsdfsf!!!”
}];
日志(“ricevo evento e Ricaro tabella”);
log($scope.data.obj);
}
servicename.subscribeMe();
$scope.$on('app.clickEvent',函数(a,b){
控制台日志(“事件”);
警报(“收到时”);
$scope.printComments();
});
}
)
您的代码中有一些错误。我把它修好了看看片段。$window对象中没有事件printComments,因此我将其更改为load,用于演示,您可以根据需要进行更改 您忘记在控制器中注入服务,因此未注册事件侦听器。此行: servicename.subscribeMe()
//创建IE+其他兼容的事件处理程序
var eventMethod=window.addEventListener?“addEventListener”:“attachEvent”;
var eventer=window[eventMethod];
var messageEvent=eventMethod==“attachEvent”?“onmessage”:“message”;
//收听来自子窗口的消息
事件器(messageEvent,函数(e){
log('parent received message!:',e.data);
$(窗口)。触发器(如数据);
},假);
var-app=angular.module('myapp',[]);
var打印操作;
函数GetFromLocalStorage(键){
var items=localStorage.getItem(键);
控制台日志(项目);
如果(项===null){
console.log(“项空”);
返回null;
}否则{
if(typeof items!=“string”){items=JSON.stringify(items);}
退货项目;
}
}
app.service('servicename',function($window,$rootScope){
函数subfunc(){
//$($window).on(“打印注释”,函数(e){
$($window).on(“打印注释”,
职能(e){
警报(“到达事件”);
$rootScope.$broadcast('app.clickEvent',e);
});
}
返回{
“subscribeMe”:子函数
};
});
应用程序控制器('MyCtrl',
函数($scope,servicename,$timeout){
$scope.data={};
$scope.printComments=函数(){
//$scope.obj=GetFromLocalStorage(“AllComments”);
$scope.data.obj=[{
“诺姆”:“第一”,
“地位”:1,
“testo”:“Rottura rullo 1!!!”
}, {
“nome”:“second”,
“状态”:0,
“testo”:“Rottura rullo fsdfsf!!!”
}];
日志(“ricevo evento e Ricaro tabella”);
log($scope.data.obj);
};
servicename.subscribeMe();
$scope.$on('app.clickEvent',函数(a,b){
控制台日志(“事件”);
警报(“收到时”);
$timeout(函数(){
$scope.printComments();
});
});
});
{{item.nome}}:
{{item.status}
{{item.testo}
您的控制器在哪里?您需要在js中定义控制器
var-app=angular.module('myapp',[]);
app.controller(“MyCtrl”,函数($scope){
$scope.obj=[{nome:“Sss”}]
})
var打印操作;
函数GetFromLocalStorage(键){
var items=localStorage.getItem(键);
控制台日志(项目);
如果(项===null){
console.log(“项空”);
返回null;
}否则{
if(项目类型!=“字符串”){
items=JSON.stringify(items);
}
退货项目;
}
}
app.service('serviceName',function($window,$rootScope){
函数subfunc(){
$rootScope.printComments=函数(){
//$rootScope.obj=GetFromLocalStorage(“AllComments”);
$r