如何过滤以@开头的单词形成角度为2的字符串

时间:2017-12-01 18:39:47

标签: javascript angular filter ionic2 startswith

我想过滤@word表单搜索字符串。搜索字符串可以是milk @company。从那个字符串我将使用'milk'作为searchTerm,我需要'@company'作为另一个搜索参数。所以我可以执行以下操作:domain.com/?searchTerm=Milk&Producer=company带有http请求。

在我的模板中:

<ion-searchbar [(ngModel)]="searchTerm" (ionInput)="searchResults(searchTerm)" [placeholder]="search" ></ion-searchbar>

searchResult函数:

searchResults() {
    let searchterm = this.searchTerm;
    let producer = // how filter @word? from this.searchTerm string

    this.searchProduct(searchTerm, producer).then(
        data => {
            console.log('data')
        }
    );
}

对api提供者的调用:

   public searchProduct(product,producer){
        var sendUrl = `http://example.com/?searchTerm=${product}$producer=${producer}`;
        this.http.get( sendUrl, { headers: new Headers(HEADER.default) })
            .map(res => res.json())
            .subscribe(data => {
                resolve(data);
        });
    }

1 个答案:

答案 0 :(得分:1)

您可以简单地使用字符串拆分方法并使用'@'作为分隔符

const searchWords = this.searchTerm.split(‘@‘)

这应该给你一个包含单词的数组。