我在 ngUpgraded AngularJS 控制器中遇到了依赖注入问题。我想在我的混合应用程序中使用 toastr 和 Handlebars.js 模块,但我每次尝试都会给我错误(handlebars 是处理很久以前编写的来自服务器端的外部脚本所必需的,这就是我需要使用它的原因一些反模式解决方案)。最后一个脚本(提供程序)仅适用于 jQuery 使用。
AngularJS 控制器:
导出常量数据可视化 = {
选择器:'dataVisualizationNgJs',
templateUrl: './data-visualization-ngjs.template.html',
绑定控制器:{
数据:'='
},
控制器:['$scope', '$timeout', ($scope, $timeout) => {
……
}
指示({
选择器:dataVisualization.selector
})
导出类 DataVisualizationFacade 扩展 UpgradeComponent {
@Input() 数据:任意;
构造函数(elementRef:ElementRef,注入器:注入器){
超级(dataVisualization.selector,elementRef,注入器);
}
}
AngularJS 模块:
export const ngjsModule = angular.module('dataVisualizationNgJs', [])
.component(dataVisualization.selector, dataVisualization).directive(DataVisualizationComponent.selector, downgradeComponent({
组件:数据可视化组件,
}) 作为 angular.IDirectiveFactory);
Angular 5 模块:
@NgModule({
声明:[
数据可视化组件,
数据可视化外观,
],
入口组件:[
数据可视化组件
],
进口:[
Ngb模块,
网关共享模块,
RouterModule.forChild(DataVisualizationRoute),
表单模块,
升级模块,
],
提供者:[
jQuery提供者,
toastrProvider,
车把供应商,
{
提供:'$范围',
useFactory: (i) => i.get('$rootScope'),
部门:['$injector']
}
]
})
导出类 DataVisualizationModule {
}
Angular 5 提供者:
export const JQUERY_TOKEN = new InjectionToken('jQuery');
导出常量 jQueryFactory = () => {
返回$;
};
导出常量 jQueryProvider = {
提供:JQUERY_TOKEN,
使用工厂:jQueryFactory
};
export const TOASTR_TOKEN = new InjectionToken('toastr');
出口常量 toastrFactory = () => {
返回烤面包机;
};
出口常量 toastrProvider = {
提供:TOASTR_TOKEN,
useFactory: toastrFactory
};
export const HANDLEBARS_TOKEN = new InjectionToken('Handlebars');
出口常量车把工厂=()=> {
返回车把;
};
出口常量车把提供者= {
提供: HANDLEBARS_TOKEN,
使用工厂:车把工厂
};