Angular6-将服务调用作为引用传递给子组件
我正在开发一个自定义选择组件,它从其父组件发出的服务调用接收数据。如果给定的调用由于任何原因失败,我需要将重试函数传递给select(服务调用本身)。调用了函数,但未按预期传递Angular6-将服务调用作为引用传递给子组件,angular,typescript,angular6,Angular,Typescript,Angular6,我正在开发一个自定义选择组件,它从其父组件发出的服务调用接收数据。如果给定的调用由于任何原因失败,我需要将重试函数传递给select(服务调用本身)。调用了函数,但未按预期传递this: 我的父组件: <div class="form-group row mb-4"> <div class="col-md-12 col-12"> <custom-select [field]="fields.bank" [formControlName]="fie
this
:
我的父组件:
<div class="form-group row mb-4">
<div class="col-md-12 col-12">
<custom-select [field]="fields.bank" [formControlName]="fields.bank.controlName" [errors]="submitted ? formControls.bank.errors : null">
</custom-select>
</div>
</div>
custom-select.component.html
<div class="select__error">
<span>
<a class="select__retry-link" (click)="retry()">retry</a>.
</span>
</div>
服务台
constructor(
public httpClient: HttpClient, //<-- it was initially private, but public also doesn't work.
) { }
public queryAll(): Observable<Bank[]> {
return this //<-- this is referring to CustomSelectComponent, not to the service itself. Throws error.
.httpClient
.get<BankResponse[]>(environment.apiUrls.banks)
.pipe(map(result => {
return this.mapToBankModels(result);
}));
}
构造函数(
公共httpClient:httpClient,//您可以进行queryAll
publicqueryall=():可观察=>{
还这个
.httpClient
.get(environment.apirls.banks)
.pipe(映射(结果=>{
返回此.mapToBankModels(结果);
}));
}
或者您可以使用Function.prototype.call()
指定此的值
retry(event?: Event): void {
if (event) { event.preventDefault(); }
if (typeof (this.retryFunction) === 'function') {
this.retryFunction();
}
}
constructor(
public httpClient: HttpClient, //<-- it was initially private, but public also doesn't work.
) { }
public queryAll(): Observable<Bank[]> {
return this //<-- this is referring to CustomSelectComponent, not to the service itself. Throws error.
.httpClient
.get<BankResponse[]>(environment.apiUrls.banks)
.pipe(map(result => {
return this.mapToBankModels(result);
}));
}
public queryAll = (): Observable<Bank[]> => {
return this
.httpClient
.get<BankResponse[]>(environment.apiUrls.banks)
.pipe(map(result => {
return this.mapToBankModels(result);
}));
}