代码之家  ›  专栏  ›  技术社区  ›  Govarthanan Venunathan

为什么我需要后缀“?“角形验证属性[副本]

  •  2
  • Govarthanan Venunathan  · 技术社区  · 8 年前
    <div *ngIf="heroForm.errors?.identityRevealed && (heroForm.touched || heroForm.dirty)" class="cross-validation-error-message alert alert-danger">
    Name cannot match alter ego.</div>
    

    上面为什么需要后缀?变成错误

    1 回复  |  直到 8 年前
        1
  •  6
  •   Muhammed Albarmavi    8 年前

    如果 heroForm heroForm.errors 将为空并访问 heroForm.errors.identityRevealed TypeError: Cannot read property 'identityRevealed' of null 所以 ?. 操作员将无法访问 identityRevealed errors 属性为空。

    没有 ?.

     *ngIf="(heroForm.errors && heroForm.errors.identityRevealed) && ..." 
    

    或者像@JB niset提到的那样简单

     *ngIf="heroForm.hasError('identityRevealed') && ..."
    

    safe-navigation-operator

    推荐文章