Angular2限制模板驱动输入字段中的空白

Angular2限制模板驱动输入字段中的空白,angular,Angular,前言:我不想使用被动形式 在Angular2中限制空白的正确方法是什么?我看到了AngularJS的答案,但我不确定是否有更好的方法在Angular2中实现这一点。假设我有: <input placeholder="alert tag (25 max)" maxlength="25" value="alert" type="text" #alertTagInput

前言:我不想使用被动形式

在Angular2中限制空白的正确方法是什么?我看到了AngularJS的答案,但我不确定是否有更好的方法在Angular2中实现这一点。假设我有:

        <input
          placeholder="alert tag (25 max)"
          maxlength="25"
          value="alert"
          type="text"
          #alertTagInput
          [(ngModel)]="alertTag" >

用户当前可以键入空白,如何限制?

尝试:

(keydown.space)="$event.preventDefault()"

试着用这个来设置角度误差

表单字段

                         <input #name
                           formControlName="account_nick"
                           id="name"
                           maxlength="90"
                           minlength="5"
                           placeholder="eg. my name "
                           (keyup)="noSpace($event)"
                           required
                           type="text"
                           >

这是类似的问题吗@有点像Bean0341,但我不想使用被动表单,我正试图坚持使用模板驱动的表单。更新了标题,使其更清晰。效果很好,我也做了
onDrag=“return false”onDrop=“return false”onPaste=“return false”
,因为我不希望用户将内容粘贴到输入框中
 this.countSpace = [];  //make a array 
 noSpace(key) { //log key and apply condition as you want 
        if (key.code === 'Space') {
            this.countSpace.push(key.code);
        }
        if (key.key.length === 1) { //if some one enter a only one value or one space 
            this.myform.controls['account_nick'].setErrors({'incorrect': true});
        }
        if (this.countSpace.length > 5) { // if name have more then 5 space 
            this.myform.controls['account_nick'].setErrors({'incorrect': true});
        }
    }