如何在来自后端的多个元素中放置一个选中的元素
How to place the one element as checked among multiple elements coming from the backend
我想从动态出现的元素循环中放置一个选中的元素(基于条件)。
.component.ts
checked(sensorname){
for (var i=0;i<this.sensorsarray.length; i++ ){
if(this.sensorsarray[i].name == this.sensors.name){
return true;
}else{
return false;
}
}
}
并且我将 html 中的函数调用为
.component.html
ul class="list-group" *ngFor ="let sensor of sensors;let i =index"
(ngModelChange)="dataChanged(selectsensor)">
<div class="custom-control custom-switch" >
<li class="list-group-item" style="padding: 5px;" > {{sensor.name}}
<span style="float: right;" >
<input type="checkbox" class="custom-control-input" id="customSwitches{{i}}" (click)="dataChanged(sensor,i)">
<label class="custom-control-label" for="customSwitches{{i}}"> </label>
</span>
</li>
</div>
</ul>
所以我想根据条件默认选中on元素。
你似乎遗漏了很多,所以我将让 checked
函数完成工作,假设 sensor
是一个元素列表
.component.ts
checked(sensorname){
for (var i=0;i<this.sensorsarray.length; i++ ){
if(this.sensorsarray[i].name == this.sensors.name){
this.sensorarray[i].checked=true
return true
}
else{
this.sensorarray[i].checked=false
return false
}
}
}
我想从动态出现的元素循环中放置一个选中的元素(基于条件)。
.component.ts
checked(sensorname){
for (var i=0;i<this.sensorsarray.length; i++ ){
if(this.sensorsarray[i].name == this.sensors.name){
return true;
}else{
return false;
}
}
}
并且我将 html 中的函数调用为
.component.html
ul class="list-group" *ngFor ="let sensor of sensors;let i =index"
(ngModelChange)="dataChanged(selectsensor)">
<div class="custom-control custom-switch" >
<li class="list-group-item" style="padding: 5px;" > {{sensor.name}}
<span style="float: right;" >
<input type="checkbox" class="custom-control-input" id="customSwitches{{i}}" (click)="dataChanged(sensor,i)">
<label class="custom-control-label" for="customSwitches{{i}}"> </label>
</span>
</li>
</div>
</ul>
所以我想根据条件默认选中on元素。
你似乎遗漏了很多,所以我将让 checked
函数完成工作,假设 sensor
是一个元素列表
.component.ts
checked(sensorname){
for (var i=0;i<this.sensorsarray.length; i++ ){
if(this.sensorsarray[i].name == this.sensors.name){
this.sensorarray[i].checked=true
return true
}
else{
this.sensorarray[i].checked=false
return false
}
}
}