I am trying to refresh an angular table when a message arrives informing new data is ready; I am trying to use this code following the hint at: Listen to window events in an Angularjs service, but the event is not even received when command:
parent.postMessage("printComments", "*");
is sent by another page in a frame.
What is the business?
<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();
});
}
)