简体   繁体   English

下拉菜单中的 Angular 4 设置选择选项

[英]Angular 4 setting selected option in Dropdown

There are several questions about this and different answers but none of them really answers the question.关于这个有几个问题和不同的答案,但没有一个真正回答这个问题。 So again:再说一遍:

Setting default of a Dropdown select by value isn't working in my case.在我的情况下,按值设置下拉选择的默认值不起作用。 Why?为什么? This is from the dynamic Form tutorial of Angular 4:这是来自Angular 4的动态表单教程:

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

It doesn't select anything.它不选择任何东西。 Available options are:可用选项有:

  • trueaaa真啊啊
  • falsebbb假bbb
  • falseccc假冒伪劣

But static true:但静态真实:

<option ... [selected]="true">...</option>

selects the last value (all true).选择最后一个值(全部为真)。 It also works with a private variable boolvar = true and using it in [selected]="boolvar"它也适用于私有变量boolvar = true并在[selected]="boolvar"使用它

I don't understand the difference between the "opt" object and the class variable.我不明白“opt”对象和类变量之间的区别。

If you want to select a value based on true / false use如果要根据真/假选择值,请使用

[selected]="opt.selected == true" [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 Angular 2 - 在下拉列表中设置选定的值

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:在组件中,您必须从 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):因此,如果您希望 select 选择一个默认值(并且可能是您想要的):

contact = {
    firstName: "CFR",
    comment: "No comment",
    subscribe: true,
    contactMethod: 2 // this id you'll send and get from backend
}

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 :现在在你的 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.这就是您在 Angular 中选择一个选项作为默认选项的方式,selected 是无用的。

Remove [selected] from option tag:从选项标签中删除 [selected]:

<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)

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.要在表单初始化时预选一个选项,必须将 select 元素的值设置为您正在迭代的数组的元素属性,并将选项的值设置为。 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).因此,select 的值必须设置为选项中项目的键属性(您希望在初始化时显示的项目)。 This will display the default of select as the option whose value matches the value you set for select.这将显示 select 的默认值作为其值与您为 select 设置的值匹配的选项。

You can achieve this by setting the value of the select element in the onInit method like so.您可以通过像这样在 onInit 方法中设置 select 元素的值来实现这一点。

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)
}

Lets see an example with Select control让我们看一个 Select 控件的例子
binded to: $scope.cboPais,绑定到:$scope.cboPais,
source: $scope.geoPaises来源:$scope.geoPaises

HTML HTML

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

JavaScript 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();
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用角度在下拉列表中选择的设置选项 - Setting option selected in dropdown list using angular 基于 DropDown 中选定选项的角度验证 - Angular Validation based on selected option in DropDown 以角度选择选项时,下拉列表不会折叠 - Dropdown does not collapse when the option is selected in angular 在动态角度下拉菜单中获取选定的选项 - Get selected option in dynamic Angular dropdown Angular2 下拉菜单恢复为之前选择的选项 - Angular2 Dropdown revert to previously selected option Angular - 默认情况下选择的选择下拉列表中的标记选项 - Angular - Mark option in select dropdown as selected by default 根据下拉菜单中的选项在文本框中设置整数值 - setting an integer value in a textbox according to the option selected from a dropdown 在下拉列表中,将 optgroup 名称设置为选定的选项文本,并且 JS 在 Firefox 中不起作用 - In a dropdown, setting optgroup name as selected option text with JS not working in Firefox 单击 angular 8 中的按钮时,如何从多个下拉列表中删除所选选项 - How to remove selected option from multiple dropdown on click a button in angular 8 在angular6中为“编辑”表单预先选择的选项(“下拉”) - pre-selected option ( Dropdown ) for Edit form in angular6
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM