有很多关于如何将Angular 1服务和工厂转换为Angular2的文档和示例,但我无法找到关于如何将ng1提供程序转换为等价物的任何内容在ng2中。什么是将Angular 1提供程序转换为Angular 2的正确方法
例提供商
function AlertService() {
this.toast = false;
this.$get = getService;
this.showAsToast = function(isToast) {
this.toast = isToast;
};
getService.$inject = ['$timeout', '$sce'];
function getService ($timeout, $sce) {
var toast = this.toast,
alertId = 0, // unique id for each alert. Starts from 0.
alerts = []
return {
factory: factory,
add: addAlert
};
function factory(alertOptions) {
var alert = {
type: alertOptions.type,
msg: $sce.trustAsHtml(alertOptions.msg),
id: alertOptions.alertId,
toast: alertOptions.toast
};
alerts.push(alert);
return alert;
}
function addAlert(alertOptions) {
alertOptions.alertId = alertId++;
var alert = this.factory(alertOptions);
return alert;
}
}
}
angular
.module('angularApp', [])
.provider('AlertService', AlertService);
什么是在2角的正确相当于这个?
是什么让一个相当于比一次更好? –
此时我不确定。我在互联网上找不到任何东西,因此对我来说坦率地说,相当于做了这个工作,并遵循ng2指导方针就足够了 – Deepu