html - Angular - 如何显示基于下拉选项的输入

标签 html angular

我希望在下拉菜单中选择“其他”选项时显示输入字段,但不知道如何-

这是我的代码

   <div class="form-group">
      <label for="college">College:</label>
      <select class="form-control" id="college" ngModel name="college_name" required>
        <option>Ajay Kumar Garg Engineering College</option>
        <option>Others- Please Specify Below</option>
      </select>
    </div>
    <div class="form-group" *ngIf="showfield">
      <label for="name">Enter College Name:</label>
      <input type="text" class="form-control" id="name" ngModel name="other_college_name" required>
    </div>

showfield = false;在.ts文件中设置

最佳答案

在你的组件中取一个变量,

selectedValue: string = '';

只需将 selectedvalue 分配给 ngModel 并使用该值来显示文本字段。

另外,options需要value属性并且需要value来存储在ngModel

<div class="form-group">
      <label for="college">College:</label>
      <select class="form-control" id="college" [(ngModel)]="selectedValue" name="college_name" required>
        <option value="college">Ajay Kumar Garg Engineering College</option>
        <option value="others">Others- Please Specify Below</option>
      </select>
 </div>
    <div class="form-group" *ngIf="selectedValue == 'others'">
      <label for="name">Enter College Name:</label>
      <input type="text" class="form-control" id="name" ngModel name="other_college_name" required>
    </div>

关于html - Angular - 如何显示基于下拉选项的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46074055/

相关文章:

javascript - 带有文本区域错误的移动 Safari scrollTo

MySQL 表导出为 HTML

javascript - 使用模板文字作为 id 从输入中获取数据

javascript - 如何使用 Javascript 将 ISO 8601 日期格式化为更易读的格式?

javascript - Angular 6 中更多字段后的过滤管道

javascript - 为什么我在使用 ngrx 进行延迟加载的 Angular 6 应用程序的单元测试中收到 "No provider for ReducerManager!"错误?

Angular 4单元测试模板解析错误:

jquery - Internet Explorer 7 css js 表格列显示错误

typescript - 两个 Angular 2 组件 View 之间的连接未更新

Angular 7 Material 扩展面板闪烁