Angular 6 - Form - 选择选项无法正确更新

问题描述 投票:0回答:1

有人能告诉我,为什么选择选项没有正确更新。但无论我改变了什么,控制台都得到了适当的价值

具有选择选项和保存按钮的表单。每当我改变时,我的视图中显示[0] [0] - [0] [0]。请问complete code is here

form name="form" (ngSubmit)="onSubmit()" #f="ngForm" novalidate>
    <div class="form-group" contenteditable="false" *ngFor="let val of mockData">
        <p>{{val.description}}</p>
        <label for="sort" class="col-sm-2 control-label"> select current type </label>
        <div class="col-sm-4">
            <select [(ngModel)]="saveData.selectedValue1" (change)="currChanged()" name="selectedValue1" >
                <option *ngFor='let d of dropDownString' [value]="d.currencyType">
{{d.currencyType}}
</option>
            </select>
        </div>
        <label for="sort" class="col-sm-2 control-label"> select max rate </label>
        <div class="col-sm-4">
            <select [(ngModel)]="saveData.selectedValue2" (change)="rateChanged()" name="selectedValue2" #selectedValue2 = "ngModel">
                <option *ngFor='let c of currencyValue' [value]="c.maxRate">
{{c.maxRate}}
</option>
            </select>
        </div>
    </div>
    <button>Save</button>
</form>

善意地在哪里犯错,什么必须改变。

注意:我只想单独保存选择选项值,并且应该没有任何来自get响应的更改。暂时使用一些硬编码数据。

提前致谢

javascript angular typescript angular-ngmodel
1个回答
3
投票

replace函数中,您需要用this.saveData.selectedValue1this.saveData.selectedValue2替换占位符。 this.saveData通过select与两个ngModel元素的值绑定。

例:

data.description = data.description.replace(this.prevSelectValue1, this.saveData.selectedValue1);

您还需要相应地更新以前选择的值:

this.prevSelectValue1 = this.saveData.selectedValue1;  

Stackblitz here

© www.soinside.com 2019 - 2024. All rights reserved.