Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular HTML选择器中服务的角度接口_Angular_Typescript_Angular10 - Fatal编程技术网

Angular HTML选择器中服务的角度接口

Angular HTML选择器中服务的角度接口,angular,typescript,angular10,Angular,Typescript,Angular10,如何通过HTML选择器提供依赖项注入?下面的答案是通过打字脚本 我想做一些类似的html从家长,并使用男性/女性员工搜索服务。父组件具有此html <app-search-component> </app-search-component> 我读了可以通过模块来做吗?但是,此模块在html中多次调用搜索组件,可能需要男性或女性员工搜索服务。一般来说,依赖项注入是一个静态过程。如果您想保持事物的静态,可以使用两个组件和,声明如下 // <app-search-

如何通过HTML选择器提供依赖项注入?下面的答案是通过打字脚本

我想做一些类似的html从家长,并使用男性/女性员工搜索服务。父组件具有此html

<app-search-component>
</app-search-component>


我读了可以通过模块来做吗?但是,此模块在html中多次调用搜索组件,可能需要男性或女性员工搜索服务。

一般来说,依赖项注入是一个静态过程。如果您想保持事物的静态,可以使用两个组件
,声明如下

// <app-search-female-component>
constructor(
    private searchService:SearchInFemaleEmployeeService
) {}

// <app-search-male-component>
constructor(
    private searchService:SearchInMaleEmployeeService
) {}

我不确定这是否有效,因为我将来可能会有20-30种类型,我只提供了一个简单的示例好的!20-30岁的孩子会和你的父母共存吗?我想知道为什么要做注射模型。为什么不让一个服务根据参数动态生成搜索端点呢?它们可能会根据web store的需要在许多不同的组件中使用,etcI试图编辑我的答案。这更符合你的需要吗?
// <app-search-female-component>
constructor(
    private searchService:SearchInFemaleEmployeeService
) {}

// <app-search-male-component>
constructor(
    private searchService:SearchInMaleEmployeeService
) {}
// <app-search-component type="male">
@Input()
public type: string;
constructor(
    private maleSearchService:SearchInFemaleEmployeeService,
    private femaleSearchService:SearchInMaleEmployeeService,
) {}
// SelectorService
constructor(
    private maleSearchService:SearchInFemaleEmployeeService,
    private femaleSearchService:SearchInMaleEmployeeService,
    ...
) {}

selectSearchService(type: string): ISearchService {
    switch (type) {
    ....
    }
}

// <app-search-component type="male">
@Input()
public type: string;
constructor(
    private selector:SelectorService,
) {}