[英]How to inject upgraded Angular 1 service/factory to Angular 2 component in ES5?
I have an Angular1 service with name, say, 'myService' 我有一个名字的Angular1服务,比如'myService'
I then upgraded it using the Angular2 UpgradeAdapter like this: 然后我使用Angular2 UpgradeAdapter升级它,如下所示:
var upgradeAdapter = new ng.upgrade.UpgradeAdapter();
upgradeAdapter.upgradeNg1Provider('myService');
Now I need to inject it to the angular2 component. 现在我需要将它注入angular2组件。 Official upgrade guide only has typescript version for now.
官方升级指南目前只有打字稿版本。 In typescript you use @Inject anotation with the service name for it like so:
在打字稿中你使用@Inject anotation和服务名称,如下所示:
export class MyComponent {
constructor(@Inject('myService') service:MyService) {
...
}
}
What is the syntax for injecting a service by name using ES5? 使用ES5按名称注入服务的语法是什么?
To complete the pixelbits' answer, you need also define my service within the providers list either: 要完成pixelbits的回答,您还需要在提供者列表中定义我的服务:
At the application level 在应用程序级别
document.addEventListener('DOMContentLoaded', function() { ng.platform.browser.bootstrap(Cmp, [MyService]); });
At the component level 在组件级别
var Cmp = ng.core. Component({ selector: 'cmp', providers: [ MyService ] }). (...) Class({ constructor: [MyService, function(service) { }] });
It depends on what you want to do: share your service instance for all elements in the Angular2 application or per component. 这取决于您想要做什么:为Angular2应用程序或每个组件中的所有元素共享您的服务实例。
This answers could give more details on dependency injection in Angular2 with ES5: Dependency Injection in Angular 2 with ES5 . 这个答案可以提供更多关于Angular2中ES5依赖注入的细节: Angular 2中的依赖注入ES5 。
Edit 编辑
In fact, there is a subtlety. 事实上,有一个微妙之处。 IN the case of upgrade you need not to bootstrap using the
ng.platform.browser.bootstrap
function but the one from the upgrade
object. 在升级的情况下,您不需要使用
ng.platform.browser.bootstrap
函数来引导,而是使用upgrade
对象中的函数。
upgrade.bootstrap(document.body, ['heroApp']);
Where heroApp
is the Angular1 module containing services and factories I want to use in the Angular2 application. 其中
heroApp
是包含我想在Angular2应用程序中使用的服务和工厂的Angular1模块。
In fact, when you call the upgradeNg1Provider
method on the upgrade, corresponding providers are registered within its associated injector. 实际上,当您在升级时调用
upgradeNg1Provider
方法时,相应的提供程序将在其关联的注入器中注册。 This means that you don't need to specify them at described above. 这意味着您不需要如上所述指定它们。
So you simply need to do that where you want to inject: 所以你只需要在你想要注入的地方做到这一点:
(...)
Class({
constructor: [ ng.core.Inject('customService'),
ng.core.Inject('customFactory'),
function(cService, cFactory) {
}]
});
Miško Hevery provides a great plunkr for this: http://plnkr.co/edit/yMjghOFhFWuY8G1fVIEg?p=preview . MiškoHevery为此提供了一个很好的吸引力: http ://plnkr.co/edit/yMjghOFhFWuY8G1fVIEg?p = preview。
Hope it helps you, Thierry 希望它对你有帮助,蒂埃里
Use the array notation for constructor DI of services: 对服务的构造函数DI使用数组表示法:
.Class({
constructor: [MyService, function (service) {
...
}],
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.