node.js - 等级计算器角度
问题描述
我正在尝试制作一个应用程序,您可以在其中使用表格输入学生的数字等级,它会根据数字要求输出字母等级:100-91 A, 90-81 B, 80-71 C, 70-61 D, <=60 F
. 我知道 Angular 不使用if-else
句子,所以我只是不知道如何让应用程序吐出类似的内容:“学生有 __ 级。” 当提交按钮被点击时。有任何想法吗?这是我的html:
<form novalidate #contactForm="ngForm" class="contact-form"
(ngSubmit)="onSubmit()">
<div class="container-form">
<mat-form-field>
<mat-select placeholder="Please select a student.">
<mat-option *ngFor="let state of states"
[value]="state.value">
{{ state.text }}
</mat-option>
</mat-select>
</mat-form-field>
<mat-form-field class="course-width"
hintLabel="Max 50 characters">
<input matInput
placeholder="Please enter the course name."
type="text"
[(ngModel)]="contact.course"
name="cs"
#cs="ngModel"
required
maxlength="50"
>
<mat-hint align="end">
{{cs.value?.length || 10}}/50
</mat-hint>
<mat-error>
<div *ngIf="cs.touched && !cs.valid">
<div *ngIf="cs.errors.required">
Course is required
</div>
</div>
</mat-error>
</mat-form-field>
<mat-form-field class="score-width">
<input matInput
placeholder="Please enter their score."
type="text"
[(ngModel)]="contact.score"
name="sc"
#sc="ngModel"
required
>
<mat-error>
<div *ngIf="sc.touched && !sc.valid">
<div *ngIf="sc.errors.required">
Score is required
</div>
</div>
</mat-error>
</mat-form-field>
</div>
<button mat-button type="reset">Clear</button>
<button type="submit" mat-button
[disabled]="contactForm.form.invalid"
class="background-primary text-floral-white"
>
Click the button to calculate your student's grade.
</button>
</form>
<div>
</div>
解决方案
在您的component.ts中,您必须onSubmit()
在此函数中具有以下函数,您需要创建规则并分配一个值以显示
组件.ts
//global variable
scoreVarToShow = 'F';
onSubmit(){
if(contact.score >= 91){
this.scoreVarToShow = 'A';
}else if(contact.score >= 81){
this.scoreVarToShow = 'B';
}else if(contact.score >= 71){
this.scoreVarToShow = 'C';
}else if(contact.score >= 61){
this.scoreVarToShow = 'D';
}else{
this.scoreVarToShow = 'F';
}
}
组件.html
<div>
Score: {{scoreVarToShow}}
</div>
推荐阅读
- database - 当我使用 MongoDB Atlas Search 时,如何处理本地 MongoDB 数据库中的查询?
- css - 为什么动画:填充属性在 css (svg) 中不起作用?
- r - 使用 glmnet 的正则化回归:组间没有区别?
- android - Android中的房间数据库问题
- python-3.x - 包含多个字符串的元组列表是否可以按未知索引按字母顺序排序?Python
- android - 关闭 Android Studio 中的模拟器时如何启动操作?
- spring-boot - 使用注解式 Resilience4j 测试 SpringBoot
- snowflake-cloud-data-platform - 无法在 Information_Schema 架构中找到表函数
- matlab - 为什么我在 Matlab 中第二次调用 *.mexw64 时会得到不同的结果?
- laravel - 如何为 Twilio 视频会议创建访问令牌