在 Angular 中自定义 select 的最佳方式?
Best way to make a custom select in Angular?
我正在使用 MEAN 堆栈 (Angular 6),我仍在寻找构建自定义和可重用 <select>
表单控件的最佳方法,该控件使用返回的字符串数组从 BE 生成所有 <option>
标签。假设我们有 3 种材料 wood
、metal
和 plastic
,返回的数组可以是以下之一(存储在 materials
变量中):
(in my example.component.ts)
form = new FormGroup({
'material' = new FormControl('')
});
get material() {return this.form.get('material');}
materials = [
{
key: "mat_wood",
value: "Wood"
},
{
key: "mat_metal",
value: "Metal"
},
{
key: "mat_plastic",
value: "Plastic"
}
]
或
(in my example.component.ts)
form = new FormGroup({
'material' = new FormControl('')
});
get material() {return this.form.get('material');}
materials = [
{"mat_wood": "Wood"},
{"mat_metal": "Metal"},
{"mat_plastic": "Plastic"}
]
我们有这个 HTML 结构:
(in my example.component.html)
<form [formGroup]="form">
<div class="select-wrap">
<span class="select-value">{{}}</span>
<select formControlName="material">
<option *ngFor="let mat of materials" value="{{}}">{{}}</option>
</select>
</div>
</form>
最终必须编译成这样:
<select formControlName="material">
<option value="mat_wood">Wood</option>
<option value="mat_metal">Metal</option>
<option value="mat_plastic">Plastic</option>
</select>
这里有一个自定义 select 结构的经典示例。 <span class="select-value">
向用户显示 selected 选项的文本。 <select>
已将 opacity
设置为 0
并位于 <span>
的顶部,因此当用户单击时,他会单击并激活它。
对于每个选项,我需要将 mat_[something]
放在 value
属性中,并将可读的 Something
作为文本放在选项中,就像上面的示例一样:
<option value="mat_wood">Wood</option>
。
问题是:如何将 selected 选项文本放入 <span>
中?我正在寻找一种可重复使用的方法。
编辑:
查看第一个答案表明(我忘了提及)使用模板变量可以完成这项工作。但是如果我们有一个封闭的 *ngFor 循环生成多个 selects,那么我们需要动态生成的模板变量。这可能吗?
一种方法是您可以引用 select 作为模板变量,例如 mySelect
.
<select #mySelect formControlName="material">
<option value="mat_wood">Wood</option>
<option value="mat_metal">Metal</option>
<option value="mat_plastic">Plastic</option>
</select>
并像这样引用它的options.text
属性。
{{ mySelect && mySelect.selectedIndex > -1 ? mySelect.options[mySelect.selectedIndex].text : ''}}
在你的 span 元素中。
您可以阅读有关模板变量的内容here
试试这个动态选项的例子
在Html
<mat-form-field>
<mat-select placeholder="Select" [(value)]="selected">
<mat-option *ngFor="let data of materials" [value]="getKey(data)">{{getValue(data)}}</mat-option>
</mat-select>
</mat-form-field>
<p>You selected: {{selected}}</p>
在TS
@Component({
selector: 'select-value-binding-example',
templateUrl: 'select-value-binding-example.html',
styleUrls: ['select-value-binding-example.css'],
})
export class SelectValueBindingExample {
selected = 'option2';
materials = [
{"mat_wood": "Wood"},
{"mat_metal": "Metal"},
{"mat_plastic": "Plastic"}
]
getValue(obj)
{
return obj[Object.keys(obj)[0]]
}
getKey(obj)
{
return Object.keys(obj)[0]
}
checkValue()
{
console.log( this["mySelect"])
}
}
我正在使用 MEAN 堆栈 (Angular 6),我仍在寻找构建自定义和可重用 <select>
表单控件的最佳方法,该控件使用返回的字符串数组从 BE 生成所有 <option>
标签。假设我们有 3 种材料 wood
、metal
和 plastic
,返回的数组可以是以下之一(存储在 materials
变量中):
(in my example.component.ts)
form = new FormGroup({
'material' = new FormControl('')
});
get material() {return this.form.get('material');}
materials = [
{
key: "mat_wood",
value: "Wood"
},
{
key: "mat_metal",
value: "Metal"
},
{
key: "mat_plastic",
value: "Plastic"
}
]
或
(in my example.component.ts)
form = new FormGroup({
'material' = new FormControl('')
});
get material() {return this.form.get('material');}
materials = [
{"mat_wood": "Wood"},
{"mat_metal": "Metal"},
{"mat_plastic": "Plastic"}
]
我们有这个 HTML 结构:
(in my example.component.html)
<form [formGroup]="form">
<div class="select-wrap">
<span class="select-value">{{}}</span>
<select formControlName="material">
<option *ngFor="let mat of materials" value="{{}}">{{}}</option>
</select>
</div>
</form>
最终必须编译成这样:
<select formControlName="material">
<option value="mat_wood">Wood</option>
<option value="mat_metal">Metal</option>
<option value="mat_plastic">Plastic</option>
</select>
这里有一个自定义 select 结构的经典示例。 <span class="select-value">
向用户显示 selected 选项的文本。 <select>
已将 opacity
设置为 0
并位于 <span>
的顶部,因此当用户单击时,他会单击并激活它。
对于每个选项,我需要将 mat_[something]
放在 value
属性中,并将可读的 Something
作为文本放在选项中,就像上面的示例一样:
<option value="mat_wood">Wood</option>
。
问题是:如何将 selected 选项文本放入 <span>
中?我正在寻找一种可重复使用的方法。
编辑:
查看第一个答案表明(我忘了提及)使用模板变量可以完成这项工作。但是如果我们有一个封闭的 *ngFor 循环生成多个 selects,那么我们需要动态生成的模板变量。这可能吗?
一种方法是您可以引用 select 作为模板变量,例如 mySelect
.
<select #mySelect formControlName="material">
<option value="mat_wood">Wood</option>
<option value="mat_metal">Metal</option>
<option value="mat_plastic">Plastic</option>
</select>
并像这样引用它的options.text
属性。
{{ mySelect && mySelect.selectedIndex > -1 ? mySelect.options[mySelect.selectedIndex].text : ''}}
在你的 span 元素中。
您可以阅读有关模板变量的内容here
试试这个动态选项的例子
在Html
<mat-form-field>
<mat-select placeholder="Select" [(value)]="selected">
<mat-option *ngFor="let data of materials" [value]="getKey(data)">{{getValue(data)}}</mat-option>
</mat-select>
</mat-form-field>
<p>You selected: {{selected}}</p>
在TS
@Component({
selector: 'select-value-binding-example',
templateUrl: 'select-value-binding-example.html',
styleUrls: ['select-value-binding-example.css'],
})
export class SelectValueBindingExample {
selected = 'option2';
materials = [
{"mat_wood": "Wood"},
{"mat_metal": "Metal"},
{"mat_plastic": "Plastic"}
]
getValue(obj)
{
return obj[Object.keys(obj)[0]]
}
getKey(obj)
{
return Object.keys(obj)[0]
}
checkValue()
{
console.log( this["mySelect"])
}
}