英文:
How do I set the default select value on an array of objects?
问题
我有一个与对象集合绑定的选择框。我想要添加一个默认值 "请选择",但似乎无法默认选择该项:
<select [(ngModel)]="ingredient.definition" class="form-select" name="ingredient{{ i }}">
<option value="" disabled selected>请选择</option>
<option *ngFor="let definition of definitions" [ngValue]="definition" [disabled]="disableOption(definition)">{{ definition.name }}</option>
</select>
<button type="button" class="btn btn-primary d-block" [disabled]="disableAdd()" (click)="addIngredient()">添加配料</button>
这是我的definition接口:
export interface Definition {
id: number;
name: string;
measurement: string;
unit: string;
measurementToUnit: number;
unitPrice: number;
}
和我的ingredient接口:
import { Definition } from './definition';
export interface Ingredient {
quantity: number;
definition: Definition;
}
选择框是动态添加的,因此在添加选择框后,我执行以下操作创建一个空对象:
addIngredient(): void {
let ingredient: Ingredient = {
quantity: 0,
definition: {} as Definition
}
this.model.ingredients.push(ingredient);
}
由于我的属性上的definition对象为空,我希望选择框会自动突出显示空的默认选项,但实际情况并非如此。
我还尝试创建一个名为 "请选择" 的虚拟 "definition" 对象并将其添加到 "definitions" 集合中,但似乎也不起作用。
有人知道使其正常工作的正确方法吗?
英文:
I have a select that binds to a collection of objects. I wanted to add a default value "Please select" but I can't seem to get it to select the item by default:
<select [(ngModel)]="ingredient.definition" class="form-select" name="ingredient{{ i }}">
<option value="" disabled selected>Please select</option>
<option *ngFor="let definition of definitions" [ngValue]="definition" [disabled]="disableOption(definition)">{{ definition.name }}</option>
</select>
<button type="button" class="btn btn-primary d-block" [disabled]="disableAdd()" (click)="addIngredient()">Add ingredient</button>
Here is my definition interface:
export interface Definition {
id: number;
name: string;
measurement: string;
unit: string;
measurementToUnit: number;
unitPrice: number;
}
and my ingredient interface:
import { Definition } from './definition';
export interface Ingredient {
quantity: number;
definition: Definition;
}
The select is dynamically added, so upon adding the select I execute the following to create an empty object:
addIngredient(): void {
let ingredient: Ingredient = {
quantity: 0,
definition: {} as Definition
}
this.model.ingredients.push(ingredient);
}
Since the definition object on my property is empty, I was hoping the select would automatically highlight the empty default option, however this isn't the case:
I've also tried creating a dummy definition
object with the name "Please select" and adding that to the definitions
collection, however that doesn't seem to work either.
Does anyone know the correct way to get this working?
Thanks
答案1
得分: 1
我遇到过几次类似的情况,这个问题是由于选项动态填充时的索引引起的。
尝试使用formControl和patchValue,而不是ngModel和ngValue。
TS:
public defaultSelectName: string = 'someOption';
public optionsForm: FormGroup = new FormGroup({
options: new FormControl(null, {validators: Validators.nullValidator})
});
public setDefaultOption(): void {
const matchedIndex= this.definitions.findIndex(definition => definition.name === defaultSelectName);
if (matchedIndex) {
this.optionsForm.get('options')!.patchValue(this.definitions[matchedIndex].name)
}
}
通过这样做,您可以明确地设置默认选择的值以及其索引。
英文:
I came across such scenarios a couple of times as this issue arises due to indexing of options dynamically populating.
Instead of ngModel and ngValue, try using formControl and patchvalue.
TS:
public defaultSelectName: string = 'someOption';
public optionsForm: FormGroup = new FormGroup({
options: new FormControl(null, {validators: Validators.nullValidator})
});
public setDefaultOption(): void {
const matchedIndex= this.definitions.findIndex(definition => definition.name === defaultSelectName);
if (matchedIndex) {
this.optionsForm.get('options')!.patchValue(this. definitions[matchedIndex].name)
}
}
by doing so, you're explicitly setting the default selected value along with its index.
答案2
得分: 0
根据@Eliseo的建议,我成功地使其工作:
<option [value]="{}" disabled>请选择</option>
非常感谢
英文:
so following on from @Eliseo's suggestion, I managed to get it to work with:
<option [value]="{}" disabled>Please select</option>
Thanks so much
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论