Angular @Output emit 仅在第一次有效
Angular @Output emit works only the first time
嗯...我在子组件上有这个:
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" 发出它就不起作用了。
你能帮帮我吗?
问题是我使用服务来设置发射器值。这不是最佳做法。我使用主题解决了这个问题:
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);
}
.
.
.
希望对大家有所帮助。
嗯...我在子组件上有这个:
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" 发出它就不起作用了。
你能帮帮我吗?
问题是我使用服务来设置发射器值。这不是最佳做法。我使用主题解决了这个问题:
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);
}
.
.
.
希望对大家有所帮助。