Http Angular2如何清空可观察流

Http Angular2如何清空可观察流,http,typescript,angular,observable,Http,Typescript,Angular,Observable,我正在使用Angular 2.0.0-beta.0和TypeScript 1.7.5 当您在搜索框中键入某个内容并在屏幕上找到并显示某个内容时,您将删除搜索输入框并希望显示空列表。它使用以下代码工作: this.searchTermStream.next(“确保您不发现任何东西”) 是否有人在不执行http请求的情况下有更好的更干净的解决方案 @Component({ selector: 'contact-search', template: ` <div

我正在使用Angular 2.0.0-beta.0和TypeScript 1.7.5

当您在搜索框中键入某个内容并在屏幕上找到并显示某个内容时,您将删除搜索输入框并希望显示空列表。它使用以下代码工作:
this.searchTermStream.next(“确保您不发现任何东西”)

是否有人在不执行http请求的情况下有更好的更干净的解决方案

@Component({
    selector: 'contact-search',
    template: `
        <div class="container">
            <div class="form-group">
                <label for="inputUser">Search</label>
                <input #inputUser (keyup)="search(inputUser.value)">
            </div>
            <ul>
                <li *ngFor="#contact of contactList | async">{{contact.name}}</li>
            </ul>
        </div>
    `
})

export class ContactSearch {

    private searchTermStream = new Subject<string>();

    private contactList: Observable<Contact[]> = this.searchTermStream
        .debounceTime(300)
        .distinctUntilChanged()
        .switchMap((value: string) => this.contactService.searchContacts(value))

    constructor(private contactService: ContactService) {}

    search(value: string) {
        if (value) {
            this.searchTermStream.next(value);
        }
        else {
            this.searchTermStream.next("makesureyoudontfindanything");
        }
    }
}
@组件({
选择器:“联系人搜索”,
模板:`
搜寻
    {{{contact.name}
` }) 导出类联系人搜索{ private searchTermStream=新主题(); private contactList:Observable=this.searchTermStream .debounceTime(300) .distinctUntilChanged() .switchMap((值:string)=>this.contactService.searchContacts(值)) 构造函数(私有contactService:contactService){} 搜索(值:字符串){ 如果(值){ this.searchTermStream.next(值); } 否则{ this.searchTermStream.next(“确保您没有发现任何东西”); } } }
您可以在调用服务之前检查值是否为空:

private contactList: Observable<Contact[]> = this.searchTermStream
    .debounceTime(300)
    .distinctUntilChanged()
    .switchMap((value: string) => 
       0 < value.length ? this.contactService.searchContacts(value) : Observable.of([]))

search(value: string) {
    this.searchTermStream.next(value);
}
private contactList:Observable=this.searchTermStream
.debounceTime(300)
.distinctUntilChanged()
.switchMap((值:字符串)=>
0
删除输入时,这不就是在联系人列表中保留最后找到的值吗?