【问题标题】:Angular 4 : ngNativeValidate is not allowed in FormAngular 4:表单中不允许使用 ngNativeValidate
【发布时间】:2018-01-23 06:25:14
【问题描述】:

我正在使用 Angular 4 构建表单,而 required 属性不起作用。我尝试按照许多答案的建议添加<form ngNativeValidate>...</form>,但我得到了:

此处不允许使用属性 ngNativeValidate。

这是我的代码:

<form #f="ngForm" (ngSubmit)="submit()" ngNativeValidate>
      <div class="form-group input-group">
        <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>

      <input class="form-control" type="text" [(ngModel)] = "email" name='email' placeholder="Email"  required/>
      </div>
  <div class="form-group">
        <button type="submit" class="btn btn-def btn-block">Submit</button>
          </div>
        </form>

【问题讨论】:

    标签: html angular forms


    【解决方案1】:

    尝试替换这个:&lt;input class="form-control" type="text" [(ngModel)] = "email" name='email' placeholder="Email" required/&gt;

    用这个:&lt;input class="form-control" type="text" ngModel #email="ngModel" name='email' placeholder="Email" required/&gt;

    所以你所有的代码应该是这样的:

    <form #f="ngForm" (ngSubmit)="submit()">
      <div class="form-group input-group">
        <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
    
      <input class="form-control" type="text" ngModel #email="ngModel" name='email' placeholder="Email"  required/>
      </div>
      <div class="alert alert-danger" role="alert" *ngIf="!email.valid  && email.touched" >
        <span *ngIf="!email.valid"> This field is required</span>
     </div>
    <div class="form-group">
        <button type="submit" class="btn btn-def btn-block">Submit</button>
          </div>
        </form>

    同时删除不必要的 ngNativeValidate,它现在应该可以工作了。

    【讨论】:

    • 如果我的解决方案对您有用,请将其标记为答案,谢谢。
    猜你喜欢
    • 1970-01-01
    • 2013-10-11
    • 1970-01-01
    • 2017-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-19
    相关资源
    最近更新 更多