如何使用 ionic 4 在输入中使用 @ 和 # 标签进行搜索?

How to search using @ and # tags in input using ionic 4?

我正在制作一个类似于 Facebook post 的 post,当我在 post 中输入 post 数据时,当我输入“@”来搜索 return 用户名,如果我先输入“#”调用离子 4 中的 API 和 return 可用标签。

这是我所做的:

page.html

  <ion-item>
    <ion-textarea rows="3" (ionInput)="searchPeople($event)" cols="20" formControlName="comment"
      placeholder="Tweet your reply" spellcheck="true" autoComplete="true" autocorrect="true" autofocus required>
    </ion-textarea>
  </ion-item>

page.ts

searchPeople(ev: any) {
  // set val to the value of the searchbar
  let val = ev.target.value;
  let firstchar = val.charAt(0);

  if (firstchar = "@") {
    console.log("search people");
  }
  else if (firstchar = "#") {
    console.log("hash tag");
  } else {
   console.log("error");
  }
}

我确实喜欢这个,但它不起作用……

HTML:

(ionInput)="searchPeople($event)"更改为(input)="searchPeople($event)"..

并在

TS: 把函数改成这样,

if (firstchar === "@") {
    console.log("search people");
  }
  else if (firstchar === "#") {
    console.log("hash tag");
  } else {
   console.log("error");
  }

此处firstchar需要检查值,不应赋值。

工作示例: https://stackblitz.com/edit/ionic-basic-form-1bjibt

working example

page.html

 <ion-item>
    <ion-textarea rows="3"  (input)="searchPeople($event.target.value)"
      placeholder="Tweet your reply" spellcheck="true" autoComplete="true" autocorrect="true" autofocus required>
    </ion-textarea>
  </ion-item>

page.ts

searchPeople(searchValue : string ) {  
console.log(searchValue);
let firstchar === searchValue;

  if (firstchar === "@") {
    console.log("search people");
  }
  else if (firstchar === "#") {
    console.log("hash tag");
  } else {
   console.log("error");
  }
}
}