对象在 Angular Form 验证中可能为“null”

Rid*_*loo 1 angular

我正在对表单进行验证,以检查输入的数字是否大于某个数字。我尝试了这个答案,但仍然遇到同样的错误。

但出现以下错误:

error TS2531: Object is possibly 'null'.
 <small style="color: red;" [hidden]="myForm.get('checkAmount').hasError?'maxlength'">
                                                                ~~~~~~~~

  src/app/withdraw/withdraw.component.ts:10:16
    10   templateUrl: './withdraw.component.html',
                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Error occurs in the template of component WithdrawComponent.
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

<form [formGroup]="myForm" class="space" method="POST" autocomplete="off">
    <input type="text" name="amount" size="230" placeholder="Amount To Withdraw" 
    formControlName="checkAmount"
    [(ngModel)]="model.Balance"
    maxlength=50
    >
    <small style="color: red;" [hidden]="myForm.get('checkAmount').hasError?'maxlength'">
        The value should not exceed the balance
    </small>
</form>
Run Code Online (Sandbox Code Playgroud)

eko*_*eko 5

?为了安全起见,您可以将运算符放入每个链接中:

[hidden]="myForm?.get('checkAmount')?.hasError('maxlength')"
Run Code Online (Sandbox Code Playgroud)