I want to place the one element as checked (based on the condition) from the loop of elements which are coming dynamically.
.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;
}
}
}
and I have called the function in html as
.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>
So I want to place the on element as checked by default based on the condition.
You left a lot out it seems so I'll just make the checked
function do the work assuming sensor
is a list of elements
.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
}
}
}