如何生成空白选项并清除角度

时间:2018-06-06 14:56:16

标签: angular

什么人,我是Angular的新人,我有一个疑问。我有一个下拉列表,我希望默认情况下有一个第一个空白元素,如"select an option",当用户选择下拉元素时,第一个字段消失。稍后在setInterval的帮助下,我试图清理我的下拉列表,不选择任何选项,并从头开始查看下拉列表。我怎样才能实现它?这是我的代码。

<select  [(ngModel)]='myselect' >
  <option [value]="" selected  >select and option</option>
  <option  *ngFor='let option of test' [value]="option.id">  
 {{option.nombre}}</option>
</select>

  title = 'app';
  myselect:any;
  test:any= [{"id": "1", "nombre":"pedro" },{"id": "2", "nombre":"yeison" }];
  ngOnInit() {
    this.timeoutFun();
  }
  timeoutFun() {
    setTimeout(function(){
    //clear my dropdown 
    this.myselect=""; 
  }, 5000);
 }

2 个答案:

答案 0 :(得分:0)

这样的事情会做到:

  <select class="form-control"
          formControlName="frequency" [(ngModel)]="frequencies[i]">
     <option selected value></option>
     <option id="{'convertFromFrequencyDropDownOption'+i}" *ngFor="let frequency of (frequency$ | async)" [ngValue]="frequency">
              {{ getConcatenatedFrequency(frequency) }}
     </option>
  </select>
关键是:

<option selected value></option>

答案 1 :(得分:0)

试试这个:

<select  [(ngModel)]='myselect'>
  <option *ngIf="myselect==''" [value]="">select an option</option>
  <option  *ngFor='let option of test' [value]="option.id">  
 {{option.nombre}}</option>
</select>

在ts:

  title = 'app';
  myselect:any = "";
  test:any= [{"id": "1", "nombre":"pedro" },{"id": "2", "nombre":"yeison" }];
  ngOnInit() {
    this.timeoutFun();
  }
  timeoutFun() {
    setTimeout(function(){
    this.myselect=""; 
  }, 5000);
 }