简体   繁体   English

Angular2模板中未呈现可观察事件

[英]Observable event not rendered in Angular2 template

I am trying to figure out why an observable event is not being rendered in the template. 我试图弄清楚为什么未在模板中呈现可观察的事件。 I am trying to help one of my workmates. 我正在努力帮助我的一位同事。 He is doing an Electron project with Angular 2 and is using JavaScript instead of TypeScript. 他正在用Angular 2做一个Electron项目,并且使用JavaScript而不是TypeScript。 This is a pain and hopefully we convert it to TypeScript soon. 这很痛苦,希望我们能尽快将其转换为TypeScript。

The variable 'eventEmitter' is bound in the template with the async pipe. 变量“ eventEmitter”通过异步管道绑定在模板中。 I have a 2 sec timer that does the emit of the event. 我有一个2秒的计时器,可以发出事件。 The template does not display anything other than the Hello message. 该模板除了显示Hello消息外不显示其他任何内容。 I have put break points in and the timer is firing and the emits are going out. 我已经设置了断点,计时器正在触发,并且发射熄灭。 I coded a subscribe and verified that the event does go out and can be received. 我对订阅进行了编码,并验证了该事件确实已经消失并且可以接收。 It is comment out now. 现在是注释掉。 Here is my app component: 这是我的应用程序组件:

var count = 0;

var eventEmitter = new ng.core.EventEmitter();

(function(app) {

var MessageList =
    ng.core.Component({
            selector: 'message-list',
            template:
            '<p>Hello {{name}}</p>' +
            '<p>{{ eventEmitter | async}}</p>'
        })
        .Class({
            constructor: function() {
                console.log("In MessageList");

                /*       eventEmitter
                 .subscribe(
                 function(data) {
                 return data;
                 },
                 function(err) {
                 console.log("EventEmitter error");
                 },
                 function() {
                 console.log("Completed eventEmitter");
                 }
                 );*/


                setInterval(function() {
                    count++;
                    eventEmitter.emit("Got an event at " + count);
                }, 2000)

                this.name = "Test EventEmitter";
            }
        });

app.StatusPage =
    ng.core.Component({
            selector: 'status-page',
            templateUrl: 'app/statuspage.html',
            directives: [MessageList]
        })
        .Class({
            constructor: function() {

            }
        });

})(window.app || (window.app = {}));

So I did resolve this problem. 所以我确实解决了这个问题。 The issue was I was getting an Electron ipc message in my angular 2 code and the zone system could not handle a call from another thread. 问题是我在我的angular 2代码中收到了Electron ipc消息,并且区域系统无法处理来自另一个线程的调用。 Because the code was not in the zone, events were not detected. 因为代码不在区域中,所以未检测到事件。 I wrapped the code in a ngzone. 我将代码包装在ngzone中。

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

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