Angular 4 setting selected option in Dropdown

前端 未结 6 1043
渐次进展
渐次进展 2020-12-13 17:34

several questions about this and diffrent answeres. But none of them realy answeres the question. So again:

Setting default of a Dropdown select by value isnt workin

相关标签:
6条回答
  • 2020-12-13 18:01

    Remove [selected] from option tag:

    <option *ngFor="let opt of question.options" [value]="opt.key">
      {{opt.selected+opt.value}}
    </option>
    

    And in your form builder add:

    key: this.question.options.filter(val => val.selected === true).map(data => data.key)
    
    0 讨论(0)
  • 2020-12-13 18:05

    To preselect an option when the form is initialized, the value of the select element must be set to an element attribute of the array you are iterating over and setting the value of option to. Which is the key attribute in this case.

    From your example.

    <select [id]="question.key" [formControlName]="question.key">
      <option *ngFor="let opt of question.options" [value]="opt.key"</option>
    </select>
    

    You are iterating over 'options' to create the select options. So the value of select must be set to the key attribute of an item in options(the one you want to display on initialization). This will display the default of select as the option whose value matches the value you set for select.

    You can achieve this by setting the value of the select element in the onInit method like so.

    ngOnInit(): void{
        myForm : new FormGroup({
           ...
           question.key : new FormControl(null)
        })
        // Get desired initial value to display on <select>
        desiredValue = question.options.find(opt => opt === initialValue)
        this.myForm.get(question.key).setValue(desiredValue.key)
    }
    
    0 讨论(0)
  • 2020-12-13 18:06

    Here is my example:

    <div class="form-group">
        <label for="contactMethod">Contact method</label>
        <select 
            name="contactMethod" 
            id="contactMethod" 
            class="form-control"
            [(ngModel)]="contact.contactMethod">
            <option *ngFor="let method of contactMethods" [value]="method.id">{{ method.label }}</option>
        </select>
    </div>
    

    And in component you must get values from select:

    contactMethods = [
        { id: 1, label: "Email" },
        { id: 2, label: "Phone" }
    ]
    

    So, if you want select to have a default value selected (and proabbly you want that):

    contact = {
        firstName: "CFR",
        comment: "No comment",
        subscribe: true,
        contactMethod: 2 // this id you'll send and get from backend
    }
    
    0 讨论(0)
  • 2020-12-13 18:09

    Lets see an example with Select control
    binded to: $scope.cboPais,
    source: $scope.geoPaises

    HTML

    <select 
      ng-model="cboPais"  
      ng-options="item.strPais for item in geoPaises"
      ></select>
    

    JavaScript

    $http.get(strUrl2).success(function (response) {
      if (response.length > 0) {
        $scope.geoPaises = response; //Data source
        nIndex = indexOfUnsortedArray(response, 'iPais', default_values.iPais); //array index of default value, using a custom function to search
        if (nIndex >= 0) {
          $scope.cboPais = response[nIndex]; //if index of array was found
        } else {
          $scope.cboPais = response[0]; //select the first element of array
        }
        $scope.geo_getDepartamentos();
      }
    }
    
    0 讨论(0)
  • 2020-12-13 18:11

    If you want to select a value as default, in your form builder give it a value :

    this.myForm = this.FB.group({
        mySelect: [this.options[0].key, [/* Validators here */]]
    });
    

    Now in your HTML :

    <form [formGroup]="myForm">
        <select [formControlName]="mySelect">
            <option *ngFor="let opt of options" [value]="opt.key">ANY TEXT YOU WANT HERE</option>
        </select>
    </form>
    

    What my code does is giving your select a value, that is equal to the first value of your options list. This is how you select an option as default in Angular, selected is useless.

    0 讨论(0)
  • 2020-12-13 18:17

    If you want to select a value based on true / false use

    [selected]="opt.selected == true"

     <option *ngFor="let opt of question.options" [value]="opt.key" [selected]="opt.selected == true">{{opt.selected+opt.value}}</option>
    

    checkit out

    Angular 2 - Setting selected value on dropdown list

    0 讨论(0)
提交回复
热议问题