下拉菜单中的Angular

下拉菜单中的Angular

本文介绍了下拉菜单中的Angular 4设置选择选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关此问题和不同答案的几个问题.但是他们没有一个真的回答这个问题.再次如此:

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

在我的情况下,设置按值选择下拉列表的默认设置无效.为什么?这是来自Angular 4的动态Form教程:

Setting default of a Dropdown select by value isnt working in my case. why?This is from the dynamic Form tutorial of 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 doesnt select anything. Available options are:

  • trueaaa
  • falsebbb
  • falseccc

但是是静态的:

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

选择最后一个值(全部为true).它还可以与私有变量boolvar = true一起使用,并在[selected]="boolvar"

selects the last value (all true).It also works with a private variable boolvar = true and using it in [selected]="boolvar"

我不了解"opt"对象和类变量之间的区别?

I dont understand the difference between the "opt" object and the class variable??

推荐答案

如果您要选择基于true/false使用的值

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>

签出

Angular 2-在下拉列表中设置所选值

这篇关于下拉菜单中的Angular 4设置选择选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 13:26