Angular2路由器 - window.opener:转发时丢失变更检测

时间:2017-04-11 16:39:08

标签: javascript angular angular2-routing prototype-programming

当我打开OAuth的弹出窗口并通过

返回时
window.opener.closeCallbackFunction();

然后我的ChangeDetection在转发页面上不再有效。

function _window(): any {
  return window;
}

@Component({
  selector: 'app-register',
  templateUrl: './register.component.html',
})
export class ConsultantRegisterComponent {

    constructor() {
    }

    openPopup() {
        let _this = this;
        let selfWindow = _window();
        let popup = lib.PopupCenter('/RegisterWithFacebook', 'Sign In', 600, 400);

        selfWindow.closeCallbackFunction = function () {
            popup.close();
            _this.forward.call(_this);
        };

    }

    forward() {
        this.router.navigate(['/register-oauth']);
    }
}

这是一个错误,还是我做错了什么?

1 个答案:

答案 0 :(得分:0)

好的,谷歌搜索了一个解决方案:只需要用NgZone来做:

function _window(): any {
  return window;
}

@Component({
  selector: 'app-register',
  templateUrl: './register.component.html',
})
export class ConsultantRegisterComponent {

    constructor(private _zone:NgZone) {
    }

    openPopup() {
        let _this = this;
        let selfWindow = _window();
        let popup = lib.PopupCenter('/RegisterWithFacebook', 'Sign In', 600, 400);

        selfWindow.closeCallbackFunction = function () {
            popup.close();
            _this.forward.call(_this);
        };

    }

    forward() {
        this._zone.run(() => {
           this.router.navigate(['/register-oauth']);
        });
    }
}