【问题标题】:Angular @Output emit works only the first timeAngular @Output emit 仅在第一次工作
【发布时间】:2017-05-24 18:05:10
【问题描述】:

嗯...我在一个子组件上有这个:

this.event.emit(true);
this.calendarProviderService.addEvent(newEvent).subscribe(
    finished => {
        if (finished == true) {
            console.log (finished);
            this.event.emit(false);
        }
    }
)

请检查“True”发射是否正常工作。在我的父组件上,我有:

  <add-event *ngIf="currentActionArea == 'addevent'" (event)="addNewEvent($event)"></add-event>

addNewEvent(event){
    console.log("Emit recibido: " + event);
    if(event == true){
        this.loaderMessage = "Guardando Evento...";
        this.loadingEvents = true;
    } else {
        this.switchButtonModel = '0';
        this.currentActionArea = null;
        this.getEvents();
    }
}

但是当订阅完成时,“false”发出它不起作用。

你能帮帮我吗?

【问题讨论】:

  • 您的代码看起来不错。您在控制台中看到console.log (finished) 了吗?
  • @AngularFrance 是的,我的朋友...
  • 没有工作的 Plunker 很难说更多

标签: angular eventemitter angular-components


【解决方案1】:

问题是我使用服务来设置发射器值。这不是最佳做法。我使用主题解决了这个问题:

import { Subject } from "rxjs/Subject";

@Injectable()
export class CalendarProviderService {

/**
 * To emit getActivityWork between two components
 */
private _activityWorkSubject = new Subject<calendarActivityWork>();
public getActivityWork = this._activityWorkSubject.asObservable();

    constructor() { }

    /**
     * To set the activity work value
     * @param calendarActivity the activity to show
     */
    doActivityWork(calendarActivity : calendarActivityWork){
        return this._activityWorkSubject.next(calendarActivity);
    }
.
.
.

希望这对任何人都有帮助。

【讨论】:

    猜你喜欢
    • 2016-02-17
    • 2017-09-19
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-12
    相关资源
    最近更新 更多