加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angular – 无法读取null的’required’属性

发布时间:2020-12-17 18:02:16 所属栏目:安全 来源:网络整理
导读:在模板中,我有一个表单,其中一部分与渲染课程列表有关: form #f="ngForm" (ngSubmit)="submit(f)" div class="form-group" label for="courseCategory" Category /label select required ngModel name="courseCategory" #courseCategory="ngModel" id="cour
在模板中,我有一个表单,其中一部分与渲染课程列表有关:

<form #f="ngForm" (ngSubmit)="submit(f)">
 <div class="form-group">
  <label for="courseCategory"> Category </label>
  <select required ngModel name="courseCategory" #courseCategory="ngModel" id="courseCategory" class="form-control">
    <option value=""></option>
    <option *ngFor="let category of categories" [value]="category.id"> // line 16
      {{category.name}}
    </option>
  </select>
  <div class="alert alert-danger" *ngIf="courseCategory.touched && courseCategory.errors.required">
    Course category is required
  </div>
 </div>
</form>

在浏览器中,当我选择一个类别并按TAB(离开下拉列表)时,我在控制台上收到此错误:

CourseComponent.html:16 ERROR TypeError: Cannot read property
‘required’ of null
at Object.eval [as updateDirectives] (CourseComponent.html:20)

你能帮我找出导致这个错误的原因吗?

Boot Code 3.3.7已安装在VS代码中.

解决方法

错误并不总是存在,所以你必须像这样定义它:

<div class="alert alert-danger" *ngIf="courseCategory.touched && courseCategory.errors?.required">

安全运算符“?”

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读