当用户点击 ember.js 中的提交按钮时,如何在文本框为空时显示错误消息?

How to show the error message when the textbox is empty when user click the submit button in ember.js?

我想知道文本框何时为空以及当用户单击提交按钮时需要显示错误消息。请帮助我。

这是我的模板register.hbs代码

      {{paper-input
        label="E-mail"
        type="email"
        value=email
        onChange=(action (mut email))
        icon="email"
      }}

  {{#paper-button raised=true primary=true onClick=(action "register")}}Register{{/paper-button}}

这是我的控制器register.js代码

email: null,


actions: {
  register() {
    var data = {
      email: this.get('email'),
    };
    var record = this.store.createRecord('register', data);
    record.save().then((response) => {
      this.set('email', null);
      this.transitionToRoute('activation');
    });
  }
}

只需输入如下内容:

if (!this.get('email').trim()){
  //your code to show some error message
  return
}

trim() 从邮件中删除可能的空格 javascript:

中的空字符串或 null 字符串是假的

More on trim

More on what evaluates to True/False for strings