skip to Main Content

I am making a post similar to a Facebook post, when I post data in the post when I type “@” to search data that return users names and if I type “#” first to call API and return available tags in ionic 4.

Here what I have done:

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

I did like this but it’s not working…

2

Answers


  1. In HTML:

    Change (ionInput)="searchPeople($event)" to (input)="searchPeople($event)"..

    And in

    TS: Change the function like,

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

    Here the firstchar needs to check for value and it should not assign value.

    Working Example: https://stackblitz.com/edit/ionic-basic-form-1bjibt

    Login or Signup to reply.
  2. 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");
      }
    }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search