Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 如何在typescript中选择复选框值?_Angular_Typescript - Fatal编程技术网

Angular 如何在typescript中选择复选框值?

Angular 如何在typescript中选择复选框值?,angular,typescript,Angular,Typescript,我目前正在使用angular2。我的html代码如下 {{color}} 我的typescript文件如下 this.colors=[“橙色”、“白色”、“红色”、“绿色”、“蓝色”、“黄色”、“青色”、“天蓝色”]; 在更改下拉列表中的任何颜色时,我调用该方法 signalSelected(事件:任意,选择:字符串){ //将新输入的值和相应的字段名添加到字典中 this.selectedUIInfo[selection]=event.currentTarget.value; } 但我

我目前正在使用angular2。我的html代码如下


{{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中的复选框在哪里??