我有一个界面:
export interface ISearchService {
search(terms: string): Observable<any>;
}
我有两个实现此接口的服务:
SearchMaleEmployeeService:
@Injectable()
export class SearchMaleEmployeeService implements ISearchService {
constructor() { }
public search(terms: string): Observable<any> {
console.log('call: SearchMaleEmployeeService.search ' + terms);
}
}
SearchFemaleEmployeeService:
@Injectable()
export class SearchFemaleEmployeeService implements ISearchService {
constructor() { }
public search(terms: string): Observable<any> {
console.log('call: SearchFemaleEmployeeService.search ' + terms);
}
}
在我的search.component中,我想调用该服务的搜索方法。
我的search.component:
export class SearchComponent {
constructor(@Inject('ISearchService') private searchService: ISearchService) { }
onSearch(terms: string) {
this.searchService.search(terms);
}
}
最后,在托管SearchComponent的组件中:
providers: [
{ provide: 'ISearchService', useValue: SearchFemaleEmployeeService}
],
当我在SearchComponent中运行onSearch方法时,出现了这个错误:
EXCEPTION: Error in ./SearchComponent class SearchComponent - inline template:0:127 caused by: this.searchService.search is not a function
我认为这是因为我试图从界面调用方法,但是当我console.log
this.searchService
时,它会返回SearchFemaleEmployeeService
,这意味着这是一个很好的实例。为什么我有这个错误?
答案 0 :(得分:2)
因为您正在使用words.each do |w|
if w == redact || redact2
print "REDACTED "
else
print w + " "
end
end
useValue
它只使用你提供的值,这是一个类,在JS中是一个函数。您应该使用{provide: 'ISearchService', useValue: SearchFemaleEmployeeService}
,然后Angular会为您创建它。
useClass