简体   繁体   English

必需和已检查的输入标签在Angular组件中不起作用

[英]required and checked input tags not working in an Angular component

I'm making a sign up form for a web application, and I'm trying to make certain fields required, and one of the radio buttons pre-checked, but neither the "required" nor the "checked" input tags seem to have any effect. 我正在为Web应用程序制作一份注册表单,并且尝试将某些字段设为必填字段,并且已选中了一个单选按钮,但是“ required”或“ checked”输入标签似乎都没有任何效果。 I am using Firefox as my browser and bootstrap css classes. 我使用Firefox作为浏览器和Bootstrap CSS类。

When I take out all the Angular related code (the ngForm, ngModel and *ngIf error messages) and just open it in the browser like pure html/css, the radio button becomes checked and the required field do act as they should. 当我取出所有与Angular相关的代码(ngForm,ngModel和* ngIf错误消息)并像在纯html / css中一样在浏览器中打开它时,单选按钮将变为选中状态,并且必填字段将按照其应有的方式工作。 So I must have made some logical mistake with my Angular code, which i am fairly new to. 所以我一定对我的Angular代码犯了一些逻辑错误,这对我来说还很新。

<div class="container">
  <div class="row">
    <div class="col-12">
      <h1>Registration</h1>
    </div>
  </div>
  <form #regForm="ngForm" method="POST" (ngSubmit)= "registrate()">
    <div class="row">
      <div class="col-sm-4">
        <label for="firstName">*First name:</label>
      </div>
      <div class="col-sm-8">
        <input
         type="text"
         id="firstName"
         class="form-control"
         name = "firstName"
         #firstName = "ngModel"
         [(ngModel)] = "user.firstName"
         required/>
      </div>
    </div>
    <div class="row">
      <div class="col-sm-4">
        <label for="lastName">*Last name:</label>
      </div>
      <div class="col-sm-8">
        <input
        type="text"
        id="lastName"
        class="form-control"
        name = "lastName"
        #lastName = "ngModel"
        [(ngModel)] = "user.lastName"
        required/>
      </div>
    </div>
    <div class="row">
      <div class="col-sm-4">
        <label for="email">*E-mail:</label>
      </div>
      <div class="col-sm-8">
        <input
        type="text"
        id="email"
        class="form-control"
        name = "email"
        #email = "ngModel"
        [(ngModel)] = "user.email"
        required/>
      </div>
    </div>
    <div class="row">
      <div class="col-sm-4">
        <label for="occupation">Occupation:</label>
      </div>
      <div class="col-sm-8">
        <input
        type="text"
        id="occupation"
        class="form-control"
        name = "occupation"
        [(ngModel)] = "user.occupation"/>
      </div>
    </div>
    <div class="row">
      <div class="col-sm-4">
        <label for="username">*Username:</label>
      </div>
      <div class="col-sm-8">
        <input
        type="text"
        id="username"
        class="form-control"
        name = "username"
        #username = "ngModel"
        [(ngModel)] = "user.username"
        required/>
      </div>
    </div>
    <div class="row" *ngIf="regForm.submitted && nameExists">
      <div class="col-sm-6" style="margin: auto">
        <p style="color: red; text-align: center">The desired username already exists</p>
      </div>
    </div>
    <div class="row">
      <div class="col-sm-4">
          <label for="password1">*Password:</label>
      </div>
      <div class="col-sm-8">
          <input
          type="password"
          id="password1"
          class="form-control"
          name = "password1"
          pattern="^(?=.{8,12}$)(?!.*(\S)\1{2})(?=.*[A-Z])(?=.*[a-z]{3})(?=.*\d)(?=.*[^a-zA-Z0-9])([a-zA-Z]\S*)$"
          #password1 = "ngModel"
          [(ngModel)] = "user.password1"
          required/>
      </div>
    </div>
    <div class="row" *ngIf="regForm.submitted && password1?.errors.pattern">
      <div class="col-sm-6" style="margin: auto">
        <p style="color: red; text-align: center">Bad password!</p>
      </div>
    </div>
    <div class="row">
      <div class="col-sm-4">
          <label for="password2">*Re-enter your password:</label>
      </div>
      <div class="col-sm-8">
          <input
          type="password"
          id="password2"
          class="form-control"
          name = "password2"
          #password2 = "ngModel"
          [(ngModel)] = "checkPassword"
          required/>
      </div>
    </div>
    <div class="row">
      <div class="col-sm-4">
          <label>Gender:</label>
      </div>
      <div class="col-sm-4">
            <label for="male">male:</label>
            <input
            type="radio"
            name="gender"
            id="male"
            value="male"
            checked
            #gender1 = "ngModel"
            [(ngModel)] = "user.gender"/>
      </div>
      <div class="col-sm-4">
            <label for="female">женски:</label>
            <input
            type="radio"
            name="gender"
            id="female"
            value="female"
            #gender2 = "ngModel"
            [(ngModel)] = "user.gender"/>
      </div>
    </div>
    <div class="row">
      <div class="col-sm-4">
          <label for="idNumber">*ID number:</label>
      </div>
      <div class="col-sm-8">
          <input
          type="text"
          id="idNumber"
          class="form-control"
          name = "idNumber"
          #idNumber = "ngModel"
          [(ngModel)] = "user.idNumber"
          required/>
      </div>
    </div>
    <div class="row">
      <div class="col-sm-4">
        <label for="question">*Security question:</label>
      </div>
      <div class="col-sm-8">
        <input
        type="text"
        id="question"
        class="form-control"
        name = "question"
        #question = "ngModel"
        [(ngModel)] = "user.question"
        required/>
      </div>
    </div>
    <div class="row">
      <div class="col-sm-4">
        <label for="answer">*Answer:</label>
      </div>
      <div class="col-sm-8">
        <input
        type="text"
        id="answer"
        class="form-control"
        name = "answer"
        #answer = "ngModel"
        [(ngModel)] = "user.answer"
        required/>
      </div>
    </div>
    <div class="row" *ngIf="regForm.submitted && (firstName.invalid || lastName.invalid || email.invalid || username.invalid || password1.invalid || password2.invalid || idNumber.invalid || question.invalid || answer.invalid)">
      <div class="col-sm-6" style="margin: auto">
        <p style="color: red; text-align: center">Input fields marked with * must be filled!</p>
      </div>
    </div>
    <div class="row">
      <div class="col-12" style="text-align: center">
        <button type="submit" class="btn btn-primary">
          Register
        </button>
      </div>
    </div>
  </form>
</div>

Note that my .ts class has these fields: user is an object with all the fields used here, checkPassword is a string used to match passwords, nameExists is a boolean that is set to false, registrate() is a function that does nothing for now. 请注意,我的.ts类具有以下字段:user是在此处使用了所有字段的对象,checkPassword是用于匹配密码的字符串,nameExists是设置为false的布尔值,registrate()是不执行任何操作的函数现在。

These attributes (required and checked) in angular should be used as shown in below examples.. 这些属性(必填和检查的)应使用角度,如以下示例所示。

<input type="text" id="test2" name="test2" [(ngModel)]="test" [required]="true">

and

<input type="checkbox" [checked]="true" />

Solved the checked tag by setting the starting value of user.gender to "male". 通过将user.gender的起始值设置为“ male”来解决选中的标签。 It was an empty string by default and checked tag seems to have been canceled out by it. 默认情况下,它是一个空字符串,并且选中的标签似乎已被其取消。

required tag seems to be working from Angular's perspective (ngModels are invalid), just does not have the "normal" html behavior - the inputs don't get a red border, and the usual "Please fill out this field" message doesn't show. 从Angular的角度来看,必需的标签似乎正在工作(ngModels无效),只是没有“正常”的html行为-输入没有红色边框,并且通常的“请填写此字段”消息没有节目。 Is this expected? 这是预期的吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM