0

I want to use the bootstrap toggle buttons(switch buttons) in angular.(The components has to come dynamically from the backend ).

I have tried but toggling the switches is not working properly.

.component.html

<div *ngFor ="let sensor of sensors;let i =index"  (ngModelChange)="dataChanged(selectsensor)" >
<div class="custom-control custom-switch">
  
    <input type="checkbox" class="custom-control-input" (click)="dataChanged(sensor,i)" id="customSwitches">
    <label class="custom-control-label" for="customSwitches">
    {{sensor.name}}  </label>

</div>
</div>

1 Answer 1

0

Can you tell us what you are trying to do in the function call dataChanged() And you are calling the same function with different parameter.

From this structure

<input type="checkbox" 
class="custom-control-input" 
(click)="dataChanged(sensor,i)" id="customSwitches">
<label class="custom-control-label" for="customSwitches">
{{sensor.name}}  </label>

to

<input
  type="checkbox"
  class="custom-control-input"
  id="customSwitches{{i}}"
/>
<label class="custom-control-label" for="customSwitches{{i}}">
{{sensor.name}}  </label>

The id of the element is duplicated n number(n - number of sensors) of times in your structure. As per standard we can't duplicate the same id. So please index of the sensor to it. now it's working for me.

Note: we don't know what is the implementation inside the function dataChanged(). That also might cause issue. We removed both the function call from both places.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, worked for me one more last requirement is that I want to activate first toggle button can you please help me
@Haritha, you can do it through attr.checked. Like below. Instead of the status variable you can have your own dynamic variable. <input type="checkbox" class="custom-control-input" id="customSwitches{{i}}" [attr.checked]="sensor.status===true?true:null" /> if shared answer helps you upvote it. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.