简体   繁体   English

如何让angular知道自定义窗口事件已被触发并需要更新检查

[英]How do I let angular know a custom window event has been fired and requires update checking

I'm using a third-party library that requires me to implement my own event listener. 我正在使用一个第三方库 ,该要求我实现自己的事件侦听器。 This is done by implementing window.onGoogleYoloLoad = function() { ... } . 这是通过实现window.onGoogleYoloLoad = function() { ... } I tried to implement it like this in my user service file: 我试图在用户服务文件中像这样实现它:

@Injectable()
export class UserService {
    public userCredentials = new EventEmitter<Credentials>();
    constructor(){
        window.onGoogleYoloLoad = function(credentials){
            this.userCredentials.emit(credentials);
        }
    }
}

Then I subscribed to the event. 然后,我订阅了该活动。 The subscribers do get notified, but the view does not get updated. 订阅者确实会收到通知,但视图不会得到更新。 It's like angular doesn't know the event happened. 就像角度不知道事件发生一样。

The callback is running outside the Angular zone. 回调在Angular区域之外运行。 Move the callback to a component and call ChangeDetectorRef.detectChanges 将回调移动到组件,然后调用ChangeDetectorRef.detectChanges

import { Component, ChangeDetectorRef } from '@angular/core';

@Component(...)
export class MyComponent {
    public userCredentials = new EventEmitter<Credentials>();
    constructor(
        private cd: ChangeDetectorRef,
        private userService: UserService
    ){
        window.onGoogleYoloLoad = function(credentials){
            this.userService.userCredentials.emit(credentials);
            this.cd.detectChanges();
        }
    }
}

Re-entering the Angular zone is another option: What's the difference between markForCheck() and detectChanges() 重新输入Angular区域是另一个选择: markForCheck()和detectChanges()有什么区别?

import { Injectable, NgZone } from '@angular/core';

@Injectable()
export class UserService {
    public userCredentials = new EventEmitter<Credentials>();
    constructor(private zone: NgZone){
        window.onGoogleYoloLoad = function(credentials){
            this.zone.run(() => {
                this.userCredentials.emit(credentials);
            })
        }
    }
}

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

相关问题 如何检查任何地方是否触发了Javascript事件? - How do I check if any Javascript event has been fired, anywhere? 如何知道窗口“加载”事件是否已经触发 - How to know if window "load" event was fired already 如何知道事件发射值在Angular 4中已更改? - How to know Event emitted value has been changed in Angular 4? jQuery - 如何在事件触发后暂时禁用onclick事件侦听器? - jQuery - How can I temporarily disable the onclick event listener after the event has been fired? 窗口本身已加载后,如何触发加载事件? - How do I trigger a loading event after the window itself has been loaded? 在datepicker元素中输入日期后,如何触发角度事件 - How do i trigger an event in angular after a date has been entered in datepicker element 如何测试在触发事件后是否调用了函数? - How to test that a function has been called after an event was fired? 在浏览器中触发事件时,事件处理程序在什么时候确定? - At what point do the event handlers get determined when an event has been fired in the browser? 打字稿特征实现:如何让通用知道它已被扩展? - Typescript Trait Implementation: how to let generic know it has been already been extended? 在Angular指令中,如何在事件删除后重新应用事件侦听器? - In an Angular directive, how do you reapply an event listener after the event has been removed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM