Angular calls redis to automatically prompt the input box

In fact, it is more perfect to use Elasticsearch. The version of redis can only match the characters at the beginning, but ELK is not yet. I am ready to learn. I will use redis to implement it first. The list of prompt information returned by redis will not be written.

page code snippet

 

	<div>
		<label for="autocomplete">Auto complete</label>
		<input id="autocomplete" type="text" (keyup)="onKey($event)" />
	</div>

 

  timeout;
  onKey(event: any) {
    // Cancel the last timeout setting
    clearTimeout(this.timeout);
    // delay 3 seconds
    this.timeout = setTimeout(() => this.getSuggestion(event), 3000);
  }

  getSuggestion(event: any) {
    const token = localStorage.getItem('token');
    const word = event.target.value;
    if (!word) {
      return;
    }
    const url = 'http://localhost:8764/api/v1/user/redis/project/autoSuggest/' + word;
    let headers: HttpHeaders = new HttpHeaders();
    headers = headers.set('Content-Type', 'application/json')
    // .set('Accept', 'application/json')
    .set('Authorization', 'Bearer ' + token)
    this.http.get(url, {headers: headers}).subscribe(data => {
      const additionalInfo = data['additionalInfo'];
      // console.log(additionalInfo)
      if (additionalInfo === null) {
        return;
      }
      const suggest = additionalInfo['suggest'];
      if (suggest) {
        console.log(suggest);
      }
    }, (error: HttpErrorResponse) => {
      console.log(error.error);
    });
  }

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326428066&siteId=291194637