简体   繁体   English

下拉框中的下拉列表Angular 5消失

[英]Drop-down list in drop-down box disappears Angular 5

I have a form in Angular 5 or when I select a value in a drop-down list it displays another one. 我在Angular 5中有一个表单,或者当我在下拉列表中选择一个值时,它会显示另一个表单。

The trouble is that when I select (see link) in the first drop-down list the value 'two' and I select another value in the drop-down list that just appears this one disappears. 麻烦的是,当我在第一个下拉列表中选择(请参阅链接)时,值“ two”,而我在下拉列表中选择了另一个值,而这个值消失了。

https://stackblitz.com/edit/angular-a4va9u?embed=1&file=src/app/app.component.html https://stackblitz.com/edit/angular-a4va9u?embed=1&file=src/app/app.component.html

(And when I select the value 'first' in the first drop-down list and again the value 'two' the one that reappears) (当我在第一个下拉列表中选择值“ first”,然后再次选择值“ two”时,该值再次出现)

I did some research and I found an interesting link that corresponds to my problem but it is with AngularJs and when I read it to my problem it does not work (in addition my drop-down list appears according to the value of the drop-down list above) AngularJS select box options disappear when an item is selected 我做了一些研究,发现一个有趣的链接与我的问题相对应,但是它与AngularJs有关,当我阅读到该链接时,它不起作用(此外,我的下拉列表根据下拉列表的值显示上面的列表) 当选中一个项目时,AngularJS选择框选项消失

Click on the link is actually very simple to understand. 点击链接实际上非常简单易懂。

If you have a solution to my problem I am a taker. 如果您有解决我问题的方法,我就是接受者。

Thank you in advance for your answers, I wish you a very good day. 预先感谢您的回答,祝您愉快。

Regards, Valentin 问候,瓦伦丁

Both the dropdowns have the same formControlName 两个下拉菜单都具有相同的formControlName

what is happening is, when you select the option "two" in the first dropdown, the *ngIf="usForm.value.type == 'two' evaluates to true and the second drop down appears. However, when you select anything in the second dropdown, because it is having same formControlName , it changes the value of type to something else, making the *ngIf="usForm.value.type == 'two' evaluate to false and therefore the second dropdown disappears. 发生的情况是,当您在第一个下拉列表中选择选项“ two”时, *ngIf="usForm.value.type == 'two'计算结果为true ,并且第二个下拉列表出现。第二个下拉列表,因为它具有相同的formControlName ,因此将type的值更改为其他值,从而使*ngIf="usForm.value.type == 'two'计算结果为false,因此第二个下拉列表消失了。

You should have a different formControlName for this select option. 您应该为此选择选项使用不同的formControlName。 Like sub-type perhaps. 也许像sub-type

<p id="champs" *ngIf="usForm.value.type == 'two'">Appears
      <select formControlName="sub_type" name="consistance">
        <option style="display:none">
        <option [value]="appears.name" *ngFor="let appear of appears">
          {{appear.name}}
        </option>
      </select>
    </p>

The dropdown in the code have the same formcontrolName . 代码中的下拉菜单具有相同的formcontrolName

      <select formControlName="type"

when you select the option "two" in the first select box, the *ngIf="usForm.value.type == 'two' becomes true and the second drop down shows up in view. But when you select anything else in the second select, because it is having same formControlName, it changes the original value of type to newly selected value. This makes *ngIf="usForm.value.type == 'two' to become false and that's the reason your second dropdown doesn't goes away. 当您在第一个选择框中选择选项“ two”时, *ngIf="usForm.value.type == 'two'变为true,并且第二个下拉菜单显示在视图中。选择,因为它具有相同的formControlName,它将type的原始值更改为新选择的值,这使*ngIf="usForm.value.type == 'two'变为false,这就是第二个下拉列表不存在的原因消失了。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM