Angular ngRepeat is a powerful feature that allows for easy implementation of

I'm trying to create a toggle button using Bootstrap, but I've run into some issues. The toggle button is not displaying correctly; it's showing as a checkbox instead. When I remove the ngRepeat directive, the toggle button works fine for a single button, but I need it to work for multiple buttons using ngRepeat. Can someone please assist me with this?

<div class="box-body" ng-controller="AutomationController">
  <div class="table-responsive">
    <table class="table table-hover ">
      <thead>
        <tr>
          <th>Device Name</th>
          <th>Status</th>
          <th>Action</th>
          <th>Edit Device Name</th>
        </tr>
      </thead>
      <tbody>
        <tr ng-repeat="device in devices">
          <td>[[device.dname]]</td>
          <td>
            <span ng-if="device.status===1">ON</span>
            <span ng-if="device.status===0">OFF</span>
          </td>
          <td>
            <input type="checkbox" checked data-toggle="toggle" ng-if="device.status===1">
            <input type="checkbox" checked data-toggle="toggle" ng-if="device.status===0">
          </td>
          <td><a href="#"><i class="fa fa-pencil-square-o"></i></a></td>
        </tr>
      </tbody>
    </table>
  </div>
</div>

Answer №1

Take a look at this demo

Consider implementing a radio button for toggling and linking it to the status column.

Answer №2

Initially, take note that the square brackets around {{device.dname}} may be causing rendering issues (not sure if this was intentional).

You currently have two checkboxes that appear conditionally based on the device status. It might be simpler to use just one checkbox.

<input type="checkbox" data-toggle="toggle" ng-model="device.status" ng-true-value="1" ng-false-value="0">

By specifying the true and false values (0 or 1), you can effectively display and update the scope variables.

If you want the checkboxes to be read-only, utilize the ng-checked directive:

<input type="checkbox" data-toggle="toggle" ng-checked="device.status" disabled="disabled">

Feel free to experiment with it using this link:

https://jsfiddle.net/r0m4n/ncfs6q5w/

Answer №3

I encountered a similar issue where I found that with the code below, only one toggle displayed correctly while the rest appeared as generic checkboxes within an AngularJS controller:

<div>
    <input type="checkbox" checked data-toggle="toggle"/>
</div>
<div ng-repeat="q in Queues">
    <input type="checkbox" checked data-toggle="toggle"/>
</div>

Later on, I discovered that by changing the code to the following, all three toggles displayed correctly:

<div ng-repeat="q in [1,2,3]">
    <input type="checkbox" checked data-toggle="toggle"/>
</div> 

The issue stemmed from the fact that the elements within ng-repeat="q in Queues" were not populated during the initial page load. To resolve this, JavaScript needed to be used after applying $scope and binding data to the page.

Attempting to call $scope.apply() within the event handler resulted in an error. Instead, adding a class (e.g., "foo") to the input elements and using the following code at the end of the ".then" $http.get event handler fixed the problem:

$timeout(function () {
    $scope.$apply(function () {
        $('.foo').bootstrapToggle();
    });
}, 0);

This ensures all newly created or bound input elements are converted into bootstrap toggle controls, resolving any timing issues. Huge thanks to Jim Hopkins for his advice on handling $scope.apply()

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The setInterval function appears to be undefined despite being explicitly defined earlier

Within the HEAD section of my code, I have defined a function like so: <script type="text/javascript" src="http://code.jquery.com/jquery-1.11.1.min.js"> function live() { $.get("http://mydomain.com/script.php", function(data){ var timer ...

The attempt to create the dashboard module was unsuccessful because of an error: [$injector:nomod] For more information, visit: http://errors.angularjs.org/1.4.9/$injector/nomod?p

I'm currently in the process of learning AngularJS and encountering an error while trying to execute my code. Here is a snippet from my code: app.js var app = angular.module('DashboardApp', [ 'dashboardService' ]) ...

Exception thrown by ASP.Net Javascript code persists despite being commented out!

In my ASP website, I have incorporated Javascript code in the HEAD section to create a custom context menu. Here is the logic that I initially used: function fileGridGrouping_ContextMenu(s, e) { if(e.objectType != "row") return; fileGridGrou ...

Issues encountered while establishing a connection to an API in React Native

When attempting to log in a user by connecting to my API, I encountered some issues. It seems that every time my laptop has a different IP address, I need to make changes in the file where the fetch or XMLHttpRequest is located in order for the login proce ...

Creating a function that writes to a file by utilizing the data input from

After running the provided code snippet, it successfully works in a standalone project. However, I am interested in making modifications to replace the variable "sample_text" with an output that is displayed in the terminal instead of being hardcoded int ...

When deciding between utilizing a Javascript animation library and generating dynamically injected <style> tags in the header, consider the pros and cons of each

We are currently in the process of developing a sophisticated single-page application that allows users to create animations on various widgets. For example, a widget button can be animated from left to right with changes in opacity over a set duration. Ad ...

The latest Chrome update has caused a decrease in rendering and loading speeds for Ajax and Angular network

It has been around two weeks since a Chrome update caused trouble for users of my angular app. Previously, the entire single page application loaded in under 4 seconds, but after the update, every user was experiencing load times exceeding 40 seconds. Surp ...

Receiving error message "[object Object]" while working with JavaScript

My current challenge involves adding an item to the shopping cart through a button click event that sends necessary data to a database table storing cart items. The issue arises with the item name, as I am encountering an error displaying [object Object] ...

The functionality of Javascript is only accessible once you open the developer tools in Chrome

I am encountering a similar issue to the one discussed here: Why does JavaScript only work after opening developer tools in IE once? However, in my case, the problem arises in Chrome 44. The JavaScript code, particularly involving XHR and setInterval, f ...

What is the best approach in JavaScript to compare and modify properties in two arrays of objects with efficiency?

Here's a method I have written in an Ecma 6 component (Salesforce Lightning Web Components for anyone interested). I am sharing it here because this is more focused on JavaScript rather than LWC. Do you think this is the optimal approach to solve this ...

Using jQuery to attach events and trigger them

Within my code, I have the following scenarios: $("#searchbar").trigger("onOptionsApplied"); And in another part of the code: $("#searchbar").bind("onOptionsApplied", function () { alert("fdafds"); }); Despite executing the bind() before the trigge ...

SequelizeIncludeError: unable to fetch data using the 'include' method

My database requests are handled using Sequelize.js, and I have set up a many-to-many relationship between two tables with a third junction table called polit_in_article. Let me walk you through my three tables: politician.js: module.exports = (sequelize ...

The jQuery method .find() is unable to locate the <option> tag and behavior unpredictably

Currently, I am working on a webpage that features a table with filtering capabilities using jQuery. The filtering functionality is achieved through a Bootstrap 4 dropdown menu where each option is assigned a specific value: <select id="inputState& ...

Checking for the existence of a row in Node.js using Sqlite3

Wondering if it's possible to verify the existence of a row using node.js and the sqlite module. I currently have this function in place, but it always returns false due to the asynchronous nature of the module. function checkIfRowExists(username, pa ...

How can I create a synchronous method in Angular that waits for a response without using subscribe, similar to how synchronous methods work in C#?

Is there a way to implement a method in Angular that behaves like a SYNC method in C# without using subscribe? The issue with subscribe is that it confines my code within the block, whereas I'd prefer to write it outside of the subscribe block. Here& ...

Utilize features from both angular-strap and ui-bootstrap to enhance your project

Is it possible to efficiently utilize components from both angular-strap and ui-bootstrap without encountering conflicts that could potentially disrupt the application's functionality? For instance: Opt for a customized version of ui-bootstrap that ...

Dealing with MySQL reconnections in a Node.js REST application

Looking for assistance with my Node.js REST API connected to MySQL. I have encountered issues with the connection closing or timing out. Can someone help me troubleshoot my code: Server.js var express = require("express"); var mysql = require("mysql"); ...

Is there a way to transition an element from a fixed layout position to an absolute layout position through animation?

I am looking to create a dynamic animation effect for a button within a form. The goal is for the button to move to the center of the form, while simultaneously undergoing a horizontal flip using a scale transform. During the midpoint of this animation, I ...

Is there a way to dynamically modify a website's default viewport settings in a mobile browser?

When viewing a website in Landscape mode, everything looks good. However, switching to Portrait mode displays the message "Screen size not supported." I decided to test this on my desktop browser and discovered that adjusting the initial-scale:1 to initial ...

Can you extract information from the XML file and modify the anchor tags?

<description> <div class="field field-name-field-image field-type-image field-label- hidden"><div class="field- items"><div class="field-item even"><a href="/news/news/vg"> ...