Angular 如何在typescript中选择复选框值?
我目前正在使用angular2。我的html代码如下Angular 如何在typescript中选择复选框值?,angular,typescript,Angular,Typescript,我目前正在使用angular2。我的html代码如下 {{color}} 我的typescript文件如下 this.colors=[“橙色”、“白色”、“红色”、“绿色”、“蓝色”、“黄色”、“青色”、“天蓝色”]; 在更改下拉列表中的任何颜色时,我调用该方法 signalSelected(事件:任意,选择:字符串){ //将新输入的值和相应的字段名添加到字典中 this.selectedUIInfo[selection]=event.currentTarget.value; } 但我
{{color}}
我的typescript文件如下
this.colors=[“橙色”、“白色”、“红色”、“绿色”、“蓝色”、“黄色”、“青色”、“天蓝色”];
在更改下拉列表中的任何颜色时,我调用该方法
signalSelected(事件:任意,选择:字符串){
//将新输入的值和相应的字段名添加到字典中
this.selectedUIInfo[selection]=event.currentTarget.value;
}
但我无法使用event.currentTarget.value获取所选颜色。请建议如何从下拉列表中获取所选颜色。您应该使用checked属性并将其分配给所选项目,如下所示
<select [(ngModel)]="selectedElement" (change)="changedval()">
<option *ngFor="let color of colors" [ngValue]="color"> {{color}}</option>
</select>
<span *ngFor="let type of types">
<input type="checkbox" [checked]="color===newElement" value="color"> {{color}}<br>
</span>
changedval(){
this.newElement=this.selectedElement;
console.log(this.newElement)
}
{{color}}
{{color}}
changedval(){
this.newElement=this.selectedElement;
console.log(this.newElement)
}
请检查以下示例
<select [(ngModel)]="selectedCity" (ngModelChange)="onChange($event)" >
<option *ngFor="let c of cities" [ngValue]="c"> {{c.name}} </option>
</select>
class App {
cities = [{'name': 'SF'}, {'name': 'NYC'}, {'name': 'Buffalo'}];
selectedCity = this.cities[1];
onChange(city) {
alert(city.name);
}
}
{{c.name}}
类应用程序{
城市=[{'name':'SF'},{'name':'NYC'},{'name':'Buffalo'}];
selectedCity=this.cities[1];
昂昌(城市){
警报(城市名称);
}
}
模拟json示例?数据如何将其标记为答案链接的Plunker中的复选框在哪里??