Mar*_*ndl 2 javascript rxjs typescript angular
我尝试根据ngx芯片调整工作示例。这是onRemoving方法示例的样子:
public onRemoving(tag: TagModel): Observable<TagModel> {
const confirm = window.confirm('Do you really want to remove this tag?');
return Observable
.of(tag)
.filter(() => confirm);
}
Run Code Online (Sandbox Code Playgroud)
现在,而不是windows.confirm我想使用具有AskQuestion以下签名的方法的自定义组件:
AskQuestion(question: string, yesCallback: () => void, noCallback?: () => void): void {
Run Code Online (Sandbox Code Playgroud)
因此,现在我有多个回调,但是ngx-chips组件期望我返回一个可观察的对象。我试图使用bindCallback方法将回调转换为可观察的:
public onRemoving(tag: TagModel): Observable<TagModel> {
const choiceCallback = (choice: boolean): TagModel=> {
if (choice)
return tag;
};
this.questionService.AskQuestion("Remove item?", () => choiceCallback(true), () => choiceCallback(false))
return Observable.bindCallback(choiceCallback);
}
Run Code Online (Sandbox Code Playgroud)
但是看来我做错了。有任何想法吗?
bindCallback()读取的定义:
给它一个类型为f(x,callback)的函数f,它将返回一个函数g,当它被调用为g(x)时将输出一个Observable。
并且您的用法不符合此描述。choiceCallback()不返回返回可观察值的函数。
改用Observable构造函数:
public onRemoving(tag: TagModel): Observable <TagModel> {
return Observable.create(observer => {
const choiceCallback = (choice: boolean) => {
if (choice) {
observer.next(tag);
}
observer.complete();
};
this.questionService.AskQuestion("Remove item?", () => choiceCallback(true), () => choiceCallback(false));
});
}Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1025 次 |
| 最近记录: |