我正在开发一个基于烤面包机通知的消息系统:
http://jasonwatmore.com/post/2017/06/25/angular-2-4-alert-toaster-notifications
我的问题很简单,我想扩展功能以允许可以在不同模板中单独定位的多个警报组件。
例如,根 app.component.html 模板将具有:
<alert root></alert>
子组件将具有:
<alert subcomponent></alert>
烤面包机教程(组件)的当前实现将针对模板中警报组件的任何实例。
如果有两个,则在调用服务时它们都会收到相同的消息。
理想情况下,我想在服务调用中添加另一个参数:
this.alertService.success(message,target);
您可以在名为 target 的警报指令组件中使用字符串输入属性。
当 alert.service 触发警报时,组件仅显示目标是否匹配:
// add this input
@Input() target: string;
ngOnInit() {
this.alertService.getAlert().subscribe((alert: Alert) => {
// add this statament
if (alert.target === this.target) {
this.alerts.push(alert);
}
});
}
/*each alert function recive an extra parameter for target, if you needed with a default value, "root" for example*/
<alert target='root'></alert>
// calling an alert
this.alertService.success(
message='operation success',
target='root'
)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句