我正在学习Angular5
以及如何将useFactory
与deps
一起使用。
目前,在我的Angular
应用中,我有一个组件和三个服务。
这是组件
// email.component.ts
import { Component, OnInit, Inject } from '@angular/core';
@Component({
...
})
export class EmailComponent implements OnInit {
private currentEmail: string;
private companyName: string;
constructor(@Inject('EMS') private emailService) { }
ngOnInit() {
this.currentEmail = this.emailService.emailServiceName;
}
}
,模板是
// email.component.html
<p>Your Current Email Service is : {{ currentEmail }}</p>
这是app.module.ts
:
...
import { EmailComponent } from './components/email/email.component';
import { EmailService } from './email-service';
import { HotMail } from './hot-mail';
import { YahooMail } from './yahoo-mail';
function HotMailOrYahooMail(x) {
if (x.isHotMail) {
return new HotMail();
} else {
return new YahooMail();
}
}
@NgModule({
...
providers: [
EmailService,
{
provide: 'EMS',
useFactory: HotMailOrYahooMail,
deps: [EmailService]
}
],
...
})
export class AppModule { }
逻辑是EmailComponent
应该从HotMail
服务或YahooMail
服务以及呈现值此逻辑位于 EmailService
,即EmailService
有一个名为isHotMail
的属性,如果其值为true 则'EMS'
应该指向HotMail
服务,否则它应指向YahooMail
服务。
以下是EmailService
export class EmailService {
constructor(public isHotMail: boolean) {
this.isHotMail = true;
}
}
和HotMail
服务:
export class HotMail {
public emailServiceName = 'HotMail by Microsoft';
}
和YahooMail
服务:
export class YahooMail {
public emailServiceName = 'YahooMail by Yahoo';
}
然而,在运行ng serve
时出现以下错误:
Uncaught Error: Can't resolve all parameters for EmailService: (?).
at syntaxError (compiler.js:485)
at CompileMetadataResolver._getDependenciesMetadata (compiler.js:15699)
at CompileMetadataResolver._getTypeMetadata (compiler.js:15534)
at CompileMetadataResolver._getInjectableMetadata (compiler.js:15514)
at CompileMetadataResolver.getProviderMetadata (compiler.js:15874)
at eval (compiler.js:15785)
at Array.forEach (<anonymous>)
at CompileMetadataResolver._getProvidersMetadata (compiler.js:15745)
at CompileMetadataResolver.getNgModuleMetadata (compiler.js:15313)
at JitCompiler._loadModules (compiler.js:34404)
我在这里缺少什么?
答案 0 :(得分:0)
我的坏,我应该用这个
<CheckBox
android:id="@+id/cbProductdown_"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
....
....
....
android:text="Product"
/>