[英]Angular EventEmitter is not emitting in subscriber
我有一個事件發射器,我想向父組件發射 object 但是事件發射器沒有在訂閱者 function 中發射
categories = [];
@Output() filteredCategory = new EventEmitter<any>();
@Output() maxiumPriceEmitter = new EventEmitter<any>();
categorySub: Subscription;
formatLabel(value: number) {
return 'R' + value;
}
constructor(private shopService: ShopService) {}
ngOnInit() {
this.initCategories();
this.filterCategories();
this.updateCategories();
}
filterCategories() {
this.shopService.filterCategories.subscribe(
(fCategory: string) => {
this.categories.map(category => {
category.checked = category.name === fCategory;
});
this.updateCategories();
});
}
initCategories() {
this.categories = [
{ name: 'dress', checked: true, displayName: 'Dresses' },
{ name: 'top', checked: true, displayName: 'Shirts' },
{ name: 'skirt', checked: true, displayName: 'Skirts/Pants' },
{ name: 'purse', checked: true, displayName: 'Purse' },
{ name: 'bag', checked: true, displayName: 'Bags' },
];
}
updateCategories() {
const categories = this.categories
.filter((category) => {
return category.checked;
});
console.log(categories);
this.filteredCategory.emit(categories);
}
事件發射器在訂閱者之外發出很好的信號,但是當它在訂閱者中時它不起作用?
父模板代碼
<app-filter-bar
(maxiumPriceEmitter) = 'updatedMaxiumPrice($event)'
(filteredCategory) = 'updateCategories($event)'
></app-filter-bar>
我也沒有在控制台中看到任何錯誤
原因之一可能是this.shopService.filterCategories
Observable/Subject 在您訂閱它之前發出值。 解決方法是將 observable 更改為 ReplaySubject/BehaviorSubject。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.