如何过滤angular中以@开头的单词形成的字符串 2

How filter a word starting with @ form a string in angular 2

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

在我的模板中:

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

搜索结果函数:

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);
        });
    }

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

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

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