[英]angular2: service with a template?
I am trying to create a service for showing alerts (in case sending a form goes wrong). 我正在尝试创建用于显示警报的服务(以防发送表单出错)。 This will be used on various forms of my project so I thought I could use an independant service for this.
这将用于我项目的各种形式,因此我认为我可以为此使用独立服务。 The problem is: for this alert I need HTML code at a certain place of my form, so what I need to know is : what's best practice for that?
问题是:对于此警报,我需要在表单的某个位置使用HTML代码,所以我需要知道的是:最佳做法是什么?
Can I define a template for my service? 我可以为我的服务定义模板吗? And how do I use this template in my form wihch uses the service?
以及如何在使用服务的表单中使用此模板?
If showing alerts means showing a popup then it should be a component. 如果显示警报意味着显示弹出窗口,则它应该是一个组件。 Components have a view that is added to the DOM.
组件具有添加到DOM的视图。
You can share a global service that allows other components, directives and services to communicate with your AlertsComponent
and pass strings or component types to be shown in the alert. 您可以共享全局服务,该服务允许其他组件,指令和服务与
AlertsComponent
通信,并传递要在警报中显示的字符串或组件类型。 For more details see https://angular.io/docs/ts/latest/cookbook/component-communication.html 有关更多详细信息,请参见https://angular.io/docs/ts/latest/cookbook/component-communication.html
I would use ViewContainerRef.createComponent()
like demonstrated in Angular 2 dynamic tabs with user-click chosen components to display arbitrary components as content of the alert if you need custom HTML support with Angular bindings in the alert content. 如果需要警报内容中带有Angular绑定的自定义HTML支持,我将使用Angular 2动态选项卡中所示的
ViewContainerRef.createComponent()
和用户单击所选组件,以将任意组件显示为警报内容。
I had a difficult time using angular2 packages for notifications then I decided to create my own notification for a system I'm working on. 我在使用angular2软件包进行通知时遇到了困难,然后决定为正在使用的系统创建自己的通知。 I scrapped online till I found Mathew Ross' Post Check it out and see if it guides you, somehow.
我一直在线报废,直到找到Mathew Ross的Post Check out,看看它是否可以以某种方式指导您。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.