Add placeholder to select tag in angular2

后端 未结 15 1370
眼角桃花
眼角桃花 2021-01-03 19:54

I have simple component that contains only form:

import {Component, FORM_DIRECTIVES, NgFor} from \'angular2/angular2\';


@Component({
  selector: \'test-com         


        
相关标签:
15条回答
  • 2021-01-03 20:43

    This is how we I do it:

    <select class="form-control" name="selectedSignierer" #selectedSignierer="ngModel" [(ngModel)]="suisseIdDetail.selectedSigniererBenutzerId" required>
      <option value="null" disabled selected>Select your option</option>
      <option *ngFor="let item of signiererCatalog.entries;" value="{{ item.id }}" >{{ item.value }}</option>
    </select>
    <div *ngIf="fb.submitted && showSignDefinition === true && !selectedSignierer.valid" class="validation-error">{{ 'ERROR_FIELD_REQUIRED' | translate:lang }}</div>
    
    0 讨论(0)
  • 2021-01-03 20:47
    <select class="form-control" [(ngModel)]="mySelect">
    <option value="-1">Placeholder text..</option>
    <!-- forloop of options heres -->
    </select>
    

    Then in your component. this.mySelect = -1;

    0 讨论(0)
  • 2021-01-03 20:48

    I know i'm late , this is how i'm doing ,

    initializeSelectOptions(){
     this.optionList = ['..' , '..' , '..'
      ];
     this.optionList.unshift('Select Options')
    }
    
    ngOnInit(){
     this.initializeSelectOptions();
    }
    

    In my template ,

    <select class='select-option' required [(ngModel)]='optionSelected' (ngModelChange)='optionChanged($event)'>
        <option class='option' *ngFor='let option of optionList' [value]="option " [disabled]="option === 'Select Options'">{{option}}</option>
    </select>
    
    0 讨论(0)
提交回复
热议问题