角度:可观察到的绑定和评估结果

问题描述 投票:0回答:1

我想有类似的结构

MyStruc {
   action2Execute:any; // see #q3

   constructor(coll: CollectionServices) {
    // see #q1
   }
}

并有一个像服务类

MyServiceClass {
  constructor(public http: HttpClient) { }

  myHTTPRequest(id:number):Observable<boolean>{
     this.http.get<boolean>('call/my/webservice/'+id)
  }
}

和控制器类等

MyControllerClass {

  constructor(private coll: CollectionServices) {}
  ...
  execHTTP() {
     new MyStruc(coll).action2Execute.apply(1);
  }

}

现在,我的目的是action2Exectue结合方法myHTTPRequest,把它和处理结果。我有两个问题

  1. 如果我设置action2Executethis.action2Execute=coll.myHTTPRequest比我有问题,当我把它称为MyControllerClass.#execHTTP()我得到它说的错误:TypeError: Cannot read property 'get' of undefined ==> httpundefined
  2. 我的另一个问题是,只要使用apply我不从HTTP请求获取从可观察的,即结果。
  3. 什么类型应该在类action2Exectue MyStruc
angular observable bind
1个回答
0
投票

我想通了如何声明它:

MyStruc {
   action2Execute:((id:number) => Observable<any>);

   constructor(coll: CollectionServices) {
     // to bind it to the Service ...
     this.action2Execute=coll.myHTTPRequest.bind(coll);
   }
}

比我可以把它作为预期只是作为一个经常可观察到:

MyControllerClass {
...
 constructor(private coll: CollectionServices) {}
  ...
  execHTTP() {
     new MyStruc(coll).action2Execute(1).subscribe(res => console.log(res));
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.