【发布时间】:2020-01-11 20:24:05
【问题描述】:
我的页面上有 5 个动态下拉菜单。我希望他们所有人都能相互同步。假设如果我在 dropdown1 中选择了值 1,那么在其他下拉列表中,即 2、3、4 和 5 值 1 应该隐藏。如果我在下一个下拉列表中选择了值 2,那么值 1 和 2 应该隐藏在其他下拉列表中,除了我选择它的下拉列表等等。我必须使用 Angular 8 在 typeScript 中编写这段代码。
我尝试了下面的代码,但它没有按预期工作。在此代码中,如果我从下拉列表 1 中选择值 1,它会正常工作,但如果我从下拉列表 2 中选择值 2,那么值 1 也会出现在其他下拉列表中在我的情况下不应该发生。
public _values1 = ["1", "2", "3", "4", "5"]; //These are values for dropdown
public _values2 = ["1", "2", "3", "4", "5"]; //These are values for dropdown
public _values3 = ["1", "2", "3", "4", "5"]; //These are values for dropdown
public _values4 = ["1", "2", "3", "4", "5"]; //These are values for dropdown
public _values5 = ["1", "2", "3", "4", "5"]; //These are values for dropdown
onChange1(e){ //This is function for 1st dropdown
const val = e.target.value;
if (val == 1){
this._values2 = ["2", "3", "4", "5"];
this._values3 = ["2", "3", "4", "5"];
this._values4 = ["2", "3", "4", "5"];
this._values5 = ["2", "3", "4", "5"];
}
if (val == 2){
this._values2 = ["1", "3", "4", "5"];
this._values3 = ["1", "3", "4", "5"];
this._values4 = ["1", "3", "4", "5"];
this._values5 = ["1", "3", "4", "5"];
}
if (val == 3){
this._values2 = ["1", "2", "4", "5"];
this._values3 = ["1", "2", "4", "5"];
this._values4 = ["1", "2", "4", "5"];
this._values5 = ["1", "2", "4", "5"];
}
if (val == 4){
this._values2 = ["1", "2", "3", "5"];
this._values3 = ["1", "2", "3", "5"];
this._values4 = ["1", "2", "3", "5"];
this._values5 = ["1", "2", "3", "5"];
}
if (val == 5){
this._values2 = ["1", "2", "3", "4"];
this._values3 = ["1", "2", "3", "4"];
this._values4 = ["1", "2", "3", "4"];
this._values5 = ["1", "2", "3", "4"];
}
};
我正在将条件写入这样的下拉值,但正如您所见,下拉列表中的所有值都会随着每个函数的运行而改变每个值。这与下拉菜单 2 的功能相同。下面是 HTML 代码:-
<select class="form-control" id="select1" (change)="onChange1($event)">
<option disabled selected>Select</option>
<option *ngFor='let v of _values1'>{{ v }}</option>
</select>
<select class="form-control" id="select2" (change)="onChange2($event)">
<option disabled selected>Select</option>
<option *ngFor='let v of _values2'>{{ v }}</option>
</select>
等等……
【问题讨论】:
-
您应该根据项目索引隐藏数组项目。不是项目价值。
-
你能给我一个关于 JSfiddle 或其他地方的例子吗?其实我是打字稿的新手。 @Ken Bekov
-
是的,我搞定了。非常感谢您提供解决方案的链接。你让我今天一整天都感觉很好。 @Eliseo
-
但是请你帮我一个忙。如果你这样做@Eliseo,你能解释一下代码吗?
标签: angular typescript angular8