html - 下拉列表不显示名称值
问题描述
我正在尝试使用 Angular 的下拉列表显示名称列表以供选择。这显然是一个下拉列表,但我的数组中的名称没有显示出来。我觉得这可能是一个小修复,我只是想念它,我对 Angular 比较陌生,请帮忙!
HTML页面///
<div class="container">
<div [hidden]="submitted">
<h1>Student Form</h1>
<form (ngSubmit)="onSubmit()" #StudentForm="ngForm">
<div class="form-group">
<label for="name">Course Name</label>
<input type="text" class="form-control" id="Coursename"
required
[(ngModel)]="model.Coursename" name="Coursename"
#inputname="ngModel" required required minlength= "10" maxlength="50">
<div *ngIf="Coursename.invalid && (inputCourseName.dirty || inputCoursename.touched)"
class="alert alert-danger">
Course Name is required
</div>
</div>
<div class="form-group">
<label for="CourseScore">Course Score</label>
<input type="text" class="form-control" id="CourseScore"
[(ngModel)]="model.score" name="CourseScore">
</div>
<div class="form-group">
<label for="Studentname">Student Name</label>
<select class="form-control" id="Studentname"
required
[(ngModel)]="model.name" name="Studentname"
#name="ngModel">
<option *ngFor="let name of names" [value]="name">{{name}}</option>
</select>
<div *ngIf="Studentname.invalid || Studentname.pristine" class="alert alert-danger">
Student Name is required
</div>
</div>
<button type="submit" class="btn btn-success" [disabled]="!StudentForm.form.valid">Submit</button>
<button type="button" class="btn btn-default" (click)="newStudent(); StudentForm.reset()">Clear</button>
<i>with</i> reset
<button type="button" class="btn btn-default" (click)="newStudent()">New Student</button>
<i>without</i> reset
.ts 页面///
import { Component, OnInit } from '@angular/core';
import { Student } from '../shared/student';
import { Router } from '@angular/router';
@Component({
selector: 'app-student-form',
templateUrl: './student-form.component.html',
styleUrls: ['./student-form.component.css']
})
export class StudentFormComponent implements OnInit {
Student: StudentFormComponent
names = ['Amritha Nair', 'Sarah Smith',
'Paul Hughman', 'Isabelle Simpkins', 'Sabrina Raj'];
model = new Student();
submitted = false;
onSubmit() { this.submitted = true; }
get diagnostic() { return JSON.stringify(this.model); }
constructor(private router: Router) { }
ngOnInit(): void {
this.model=new Student();
}
cancel(): void {
this.router.navigateByUrl('home');
}
}
解决方案
控制台中有一个明显的错误应该可以帮助您。
将参考名称更改#studentName
为#name
<select class="form-control" id="Studentname"
required
[(ngModel)]="model.name" name="Studentname"
#Studentname="ngModel">
<option *ngFor="let name of names" [value]="name">{{name}}</option>
</select>
推荐阅读
- linux - 使用 Cargo 构建 openssl 板条箱的问题
- python - 如何使用 ctypes 从 Python 代码中获取 char 指针的值
- html - 页脚不会停留在页面底部
- c# - 在 UWP 中使用 MIDI
- android - 如何使用 2 列卡片创建网格 [Nativescript 和 Angular]
- json - 使用 axios 时缺少键值
- javascript - Javascript正则表达式查找字符串并从整个字符串中提取它
- ruby - 在文件名中的某个关键字后面附加文本
- c# - 从部署在 IIS 上的 MVC 应用程序调用 Selenium Firefox 时无法打开
- sql - 如何测量在 Microsoft Access 的 VBA 函数中运行的多个查询的查询时间?