运算符“>”不能应用于类型 'string' 和 'number' 错误

Operator '>' cannot be applied to types 'string' and 'number' error

我在组件 HTML 文件的 angular 中使用此代码。但是当 ng serve 然后错误显示

运算符“>”不能应用于类型 'string' 和 'number'

如何解决这个问题

我的angularhtml代码是

<input type="text" #age (keyup)="0" />

<div *ngIf="age.value>18 ; else elseblock">
    You are elegable for vote
</div>

<ng-template #elseblock>
    <div>You are not elegable for vote</div>
</ng-template>

错误是

Error: src/app/app.component.html:88:17 - error TS2365: Operator '>' cannot be applied to types 'string' and 'number'.

88 <div *ngIf="age.value>18 ; else elseblock"> ~~~~~~~~~~~~

src/app/app.component.ts:9:16 9 templateUrl: './app.component.html', ~~~~~~~~~~~~~~~~~~~~~~ Error occurs in the template of component AppComponent.

<input> 默认提供字符串类型的值。您可以明确地将其转换为数字以执行比较。

<input type="text" #age (keyup)="0" />

<div *ngIf="+age.value > 18; else elseblock">     <!-- notice the '+' -->
  You are elegable for vote
</div>

<ng-template #elseblock>
  <div>You are not elegable for vote</div>
</ng-template>

你也可以有 type="number",因为年龄必须是一个数字,但如果没有显式类型转换,它对比较没有帮助。