Change Detect not working in directive event ouput in Angular 2

前端 未结 2 1260
臣服心动
臣服心动 2021-01-22 06:28

I use this directive. However, on the setAddress event output, no changes are detected in my component. The view is not updated. I d\'ont understand.

For test, if i remo

2条回答
  •  礼貌的吻别
    2021-01-22 06:47

    Another possible solution is to inject ApplicationRef and call tick() method to run change detection cycle:

    export class GoogleplaceDirective  {
    
      constructor(private app: ApplicationRef, ...) {
        ...
      }
    
      invokeEvent(place:Object) {
        this.setAddress.emit(place);
        this.app.tick(); <-----------------------------------
      }
    

    This is essentially what zone is doing:

    _this._zone.onMicrotaskEmpty.subscribe({
        next: function () {
            _this._zone.run(function () {
                _this.tick();
            });
        }
    });
    

提交回复
热议问题