gpt4 book ai didi

angular - 如何在 Angular 2 应用程序中使用 typescript 显示基于其他下拉值的下拉值

转载 作者:太空狗 更新时间:2023-10-29 17:24:48 26 4
gpt4 key购买 nike

我正在开发 Angular 2 应用程序,在我当前的项目中,我想要显示基于年份、品牌、型号的下拉值的功能。例如,如果我选择本田,则在型号下拉列表中仅显示本田型号。

默认情况下,我将绑定(bind)年份、品牌和型号的完整数据。

这是我的看法。

enter image description here

VehicleComponent.html

<div class="row form-group">
<div class="col-md-3">
<label class="control-label">Year*</label><br />
<select friendly-name="Year" id="year" name="year" class="col-md-6 form-control" ng-required="true" onchange='OnChange()' >
<option>Select</option>

<option *ngFor='let type of lookupdetailsvehicleyearinfo'>{{type.LookupValue}}</option>

</select>
</div>
<div class="col-md-3">
<label class="control-label">Make*</label><br />
<select friendly-name="Make" class="col-md-6 form-control" ng-required="true" >
<option>Select</option>
<option *ngFor='let type of lookupdetailsvehiclemakeinfo'>{{type.LookupValue}}</option>
</select>
</div>
<div class="col-md-3">
<label class="control-label">Model*</label>
<select friendly-name="Model" class="col-md-6 form-control" ng-required="true" >
<option>Select</option>
<option *ngFor='let type of lookupdetailsvehiclemodelinfo'>{{type.LookupValue}}</option>
</select>
</div>
</div>

我将从我自己的 API 中获取上述数据。

能否请您告诉我如何在 Angular 2 应用程序中为上述功能编写 typescript 代码。

最佳答案

您唯一需要做的就是跟踪您的选择输入的 change 事件并更改另一个选择输入的源。 Angular2 将完成剩下的工作。

使用选定的:

@Component({
selector: 'my-app',
template: `
<div>
<h2>Hello {{name}}</h2>

<select (change)="firstDropDownChanged($event.target.value)" >
<option>Select</option>
<option *ngFor='let v of _values1'>{{ v }}</option>
</select>

<select >
<option>Select</option>
<option *ngFor='let v of _values2'>{{ v }}</option>
</select>
</div>
`,
})
export class App {
name:string;

private _values1 = ["1", "2", "3"];
private _values2 = [];

constructor() {
this.name = 'Angular2'
}

firstDropDownChanged(val: any) {
console.log(val);

if (val == "1") {
this._values2 = ["1.1", "1.2", "1.3"];
}
else if (val == "2") {
this._values2 = ["2.1", "2.2", "2.3"];
}
else if (val == "3") {
this._values2 = ["3.1", "3.2", "3.3"];
}
else {
this._values2 = [];
}
}
}

现场演示:https://plnkr.co/edit/GDXsPt4aiS7vus6oPvuU?p=preview

更新

或者您可以使用selectedIndex: (请注意,-1 会导致您的第一个“选择”项。

@Component({
selector: 'my-app',
template: `
<div>
<h2>Hello {{name}}</h2>

<select (change)="firstDropDownChanged($event.target.selectedIndex - 1)" >
<option>Select</option>
<option *ngFor="let v of _values1">{{ v.val }}</option>
</select>

<select >
<option>Select</option>
<option *ngFor='let v of _values2'>{{ v }}</option>
</select>
</div>
`,
})
export class App {
name:string;

private _values1 = [
{ id: 1, val: "huhu" },
{ id: 2, val: "val2" },
{ id: 3, val: "yep" },
{ id: 4, val: "cool" }
];
private _values2 = [];

constructor() {
this.name = 'Angular2'
}

firstDropDownChanged(val: any) {
const obj = this._values1[val];
console.log(val, obj);

if (!obj) return;

if (obj.id == 1) {
this._values2 = ["1.1", "1.2", "1.3"];
}
else if (obj.id == 2) {
this._values2 = ["2.1", "2.2", "2.3"];
}
else if (obj.id == 3) {
this._values2 = ["3.1", "3.2", "3.3"];
}
else {
this._values2 = [];
}
}
}

现场演示:https://plnkr.co/edit/wugNC6S27FB48EtRN906?p=preview

关于angular - 如何在 Angular 2 应用程序中使用 typescript 显示基于其他下拉值的下拉值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40974191/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com