【发布时间】:2020-08-06 00:04:30
【问题描述】:
我试图在我的货币转换器中交换两个选择选项值。我有一个链接图标,它激活了交换两个值的 swapOptions 函数。这部分工作正常。问题是如何将这个新值设置为“选择”元素?
这是我的打字稿功能,我可以在控制台中看到应该在选择选项中设置的新值:
startSwap:any;
endSwap: any;
swapOptions(firstOption: any, secondOption: any){
for(let i = 0; i < this.parsedJson.length; i++){
if(firstOption == this.parsedJson[i].Srednji){
this.endSwap = this.parsedJson[i].SelectOption;
}
else if(secondOption == this.parsedJson[i].Srednji){
this.startSwap = this.parsedJson[i].SelectOption;
}
}
console.log("New start option : "+this.startSwap)
console.log("New end option : "+this.endSwap)
}
这是我的html。我曾尝试使用 [(ngModel)] 但在更改时我得到空的选择元素。
<select name="options" #firstSelect id="slc" class="form-control" [(ngModel)]="startSwap">
<option *ngFor="let a of parsedJson;" class="form-control" (click)="firstSelectValues(a.Jedinica, a.Valuta)" (click)="intervalCurrencyStats()" [(value)]="a.Srednji">{{a.SelectOption}}</option>
</select>
<p id="p">End value: <a href="#converter" style="cursor: pointer;" (click)="swapOptions(firstSelect.value, secondSelect.value)" ><i class="fa fa-arrows-v"></i></a></p>
<select name="options2" #secondSelect id="slc" class="form-control" [(ngModel)]="endSwap">
<option *ngFor="let a of parsedJson;" class="form-control" (click)="secondSelectValues(a.Jedinica, a.Valuta)" [(value)]="a.Srednji">{{a.SelectOption}}</option>
</select>
在这张图片上,您可以看到两个选择选项元素和链接到 swapOption 函数的蓝色箭头。空白字段是单击箭头的结果。
欢迎任何建议。提前致谢!
Angular CLI:9.1.8 节点:12.13.0
【问题讨论】:
-
试试:swapOptions() { const cloned = Object.assign({}, this.startSwap); this.startSwap = this.endSwap; this.endSwap = 克隆; }
标签: html angular typescript