2017-10-21 46 views
0

我目前有一个移动应用程序,您可以搜索某些项目,当前搜索功能的工作方式是当你按空间这将被创建成一个标签。而不是当用户按下空格键时,我希望它在用户按下移动键盘上的'go/search/enter'时创建标签。离子 - 创建标签时,用户按下去/键盘上输入

下面的代码是我当前使用的函数,当“”存在时创建标签。这需要改变,因为有些项目是2个字,例如柴种子

constructor(public keyboard: Keyboard, public formBuilder: FormBuilder, public navCtrl: NavController, public navParams: NavParams, public apiAuthentication: ApiAuthentication, private http: Http) { 
    this.tags = []; 
    this.myForm = this.formBuilder.group({ 
     tags: [''] 
    }); 
    this.myForm.get('tags') 
     .valueChanges 
     .subscribe((value: string) => { 
     if(value.indexOf(' ') > -1) { 
      let newTag = value.split(' ')[0]; 
      console.log(newTag); 
      if(newTag) { 
      this.tags.push(newTag); 
      this.myForm.get('tags').setValue(''); 
      } 
      this.searchRecipeDB(this.tags); 
     } 
     }); 
    } 

我无法工作,如何在旅途中/输入移动设备上的按钮的作品,所以如果你能告诉我什么去改变它会很棒。

感谢,

回答

0

假设你使用离子输入,离子textarea的,你可以听按键事件:

<ion-input (keypress)="onPress($event.which) ... > 

.TS:

onPress(keyCode: number): void { 
if(keyCode === 13) { 
    // the keyCode for return/go/enter is 13 (iphone's) 
    doWhateverYouWant(); 
} 
} 

可以在这里搜索其他keyCodes: https://www.w3schools.com/charsets/ref_utf_basic_latin.asp

相关问题