简体   繁体   English

$ window.addEventListener未在角度触发

[英]$window.addEventListener not triggered in angular

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: 我试图按照以下提示使用此代码: 侦听Angularjs服务中的窗口事件 ,但是在命令时甚至没有收到该事件:

 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();
                    });
                }
            )

You had some error in you code. 您的代码中有一些错误。 I fixed it look at the snippet. 我将其固定在代码段中。 there is no event printComments in $window object so i changed it to load for demo purpose, you could change it according to your need. $ window对象中没有事件printComments ,因此我将其更改为出于演示目的而加载 ,您可以根据需要进行更改。

You forgot to inject the service in your controller, so your event listener was not registered.this line: 您忘记将服务注入到控制器中,因此未注册事件侦听器。此行:

servicename.subscribeMe(); servicename.subscribeMe();

 // Create IE + others compatible event handler var eventMethod = window.addEventListener ? "addEventListener" : "attachEvent"; var eventer = window[eventMethod]; var messageEvent = eventMethod == "attachEvent" ? "onmessage" : "message"; // Listen to message from child window eventer(messageEvent,function(e) { console.log('parent received message!: ',e.data); $(window).trigger(e.data); },false); 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).on("printComments", function(e){ $($window).on("printComments", function(e) { alert("arriva evento"); $rootScope.$broadcast('app.clickEvent', e); }); } return { "subscribeMe": subsFunc }; }); app.controller('MyCtrl', function($scope,servicename,$timeout) { $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"); $timeout(function(){ $scope.printComments(); }); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div ng-controller="MyCtrl" ng-app="myapp"> <table servicename> <tr ng-repeat="item in data.obj track by $index"> <td class="plantCell">{{item.nome}}: </td> <td class="statusCell">{{item.status}}</td> <td class="statusCell">{{item.testo}}</td> </tr> </table> <iframe src="//sample ifream with postmessage script"></iframe> </div> 

where is your controller? 您的控制器在哪里? you need to define the controller in the js 您需要在js中定义控制器

 var app = angular.module('myapp', []); app.controller("MyCtrl", function($scope) { $scope.obj = [{nome:"Sss"}] }) 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() { $rootScope.printComments = function() { //$rootScope.obj=GetFromLocalStorage("AllComments"); $rootScope.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($rootScope.obj); } $window.addEventListener('printComments', function(e) { console.log("ricevo evento"); $rootScope.printComments(); }) } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="myapp" ng-controller="MyCtrl"> <table ng-table="commentsTable"> <tr ng-repeat="item in obj track by $index"> <td class="plantCell">{{item.nome}}: </td> <td class="statusCell">{{item.status}}</td> <td class="statusCell">{{item.testo}}</td> </tr> </table> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM