【问题标题】:search with async source (Angular2 promise)使用异步源搜索(Angular2 承诺)
【发布时间】:2016-10-18 20:51:21
【问题描述】:

我正在使用Semantic-UISearch 组件和Angular2 承诺。 这是我的代码:

foo.component.ts

export class FooComponent implement OnInit {
    myData:any = [];
    ...

    ngOnInit() {
        this.myDataService.getData().then(myData => this.myData = myData);
        $('.ui.search').search({ source: this.myData });
    }
}

foo.component.html

<div class="ui search">
    <input class="prompt" type="text">
    <div class="results"></div>
</div>

source 始终为空。我认为这是由于对数据服务的异步调用而发生的。

你知道我该如何解决这个问题吗?

【问题讨论】:

    标签: search asynchronous angular promise semantic-ui


    【解决方案1】:

    fstanis 的回答在技术上是正确的。但我对他错过的还有一点补充。 OP 将结果保存在组件级属性中,而不是直接使用返回的结果,可能供以后使用。我还在此答案中添加了另一种实现此目的的可能方法。

    1) OP 尝试这样做的方式

    export class FooComponent implement OnInit {
        myData:any = [];
        ...
    
        ngOnInit() {
            this.myDataService.getData().then(myData => {
                this.myData = myData;
                $('.ui.search').search({ source: this.myData });
            });
        }
    }
    

    2) 使用 Angular 2 订阅

    export class FooComponent implement OnInit {
        myData:any = [];
        ...
    
        ngOnInit() {
            this.myDataService.getData().subscribe(
                data => {
                  this.myData = data;
                  $('.ui.search').search({ source: this.myData });
                },
                error => {
                  // Write your logic here if an error was occurred. Use the variable "error" which has info about what happened.
                },
                () => {
                  // This fires when the call has been finished. Just like .complete in jQuery.get or jQuery.post. So that you can do necessary operations after everything is completed.
                }
            );
        }
    }
    

    你的服务应该是这样的

    import { Injectable } from '@angular/core';
    import { Http } from '@angular/http';
    import {Observable} from 'rxjs/Rx';
    import 'rxjs/add/operator/map';
    import 'rxjs/add/operator/catch';
    
    @Injectable()
    export class MyDataService {
    
      constructor(private http:Http) { }
    
      getData(){
        return this.http.get('some/api/url').map(
            res => res.json()
        );
      }
    
    }
    

    希望这会有所帮助。

    【讨论】:

    • 感谢您的帮助,但两种解决方案均无效。搜索弹出框为空。
    • 您是否检查过您的 Angular 2 服务是否正确地从服务器获取数据?让我更新我的答案,包括一个服务应该是什么样子的示例。
    • 使用 Semantic-UI 搜索组件的mockResponse 工作。
    • 您的解决方案使用Observable 而不是Promise
    • 好吧,我确实提供了多种方法来实现您的最终结果。
    【解决方案2】:

    现在,您正在使用初始值(一个空数组)初始化搜索,因为运行 ngOnInit 时数据尚未到达。

    对于这种特殊情况,最简单的解决方案是在初始化搜索组件之前等待您的数据到达:

    export class FooComponent implement OnInit {
        ...
    
        ngOnInit() {
            this.myDataService.getData().then(myData => {
                $('.ui.search').search({ source: myData });
            });
        }
    }
    

    请记住,Semantic UI 的搜索组件有一个内置的机制来保持远程搜索结果 - 您可能需要在 usage section of the docs 中查看它,看看您是否可以使用它。

    【讨论】:

    • 我确实在文档中阅读了该链接,但我无法弄清楚如何将“Angular2 方式”(承诺)应用于搜索组件。你能帮帮我吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-01
    • 2017-06-15
    相关资源
    最近更新 更多