Error: API Not Responding to Switch Button Commands

I'm encountering an issue with a switch button that is based on an API. Initially, the button changes from "on" to "off" in my view when clicked. However, when I click the button to turn it back "on", it doesn't work. What's even more peculiar is that after clicking it once, the button becomes unresponsive. It's quite perplexing. Does anyone have a solution for this? Your help would be greatly appreciated.

Controller :

This controller is responsible for displaying whether the isactive API is turned on or off, and it displays accordingly based on isactive.

$scope.result[item._id] =   {
     item : item,
     active : item.active,
     btn_label : item.active === 0 ? "On" : "Off"
};

As a result, I have included the btn_label in the view like this:

<tbody ng-repeat="data in result">
                    <tr>
                        <td>
                            {{ startnumber + $index + 1 }}
                        </td>
                        <td>
                            {{ data.item._id }}
                        </td>
                        <td>
                            {{ data.item.title }}
                        </td>
                        <td>
                            {{ data.item.category.label }}
                        </td>
                        <td>
                            {{ data.item.user.name }}
                        </td>
    <button class="btn btn-success" ng-click="FiturThread(data.item)"><i class="fa fa-toggle-on"></i> {{ data.btn_label }}</button>
</tbody>

My assumption was to simply change the btn_label in the $scope using FiturThread, but it did not work as expected.

$scope.FiturThread = function(data) {

        if(data.btn_label === 0){
            data.btn_label = "Off";
        }else{
            data.btn_label = "On";
        }
};

If you have any insights or solutions, please share them with me. Thank you!

Answer №1

Your data.btn_label is always not equal to zero, which means the "on" label is never displayed. Could it be that you dismissed it with the active property?

My suggestion would be to utilize a boolean active property like this:

...
active: item.active,
btn_label : item.active ? "On" : "Off"
...

$scope.FiturThread = function(data) {
    data.active = !data.active;
    data.btn_label = data.active ? "On" : "Off"
};

This approach is much more concise and easy to understand.

Additionally, ensure you send the data to the controller method using : ng-click="FiturThread(data)"

Answer №2

Would you mind giving this a shot:

$scope.ToggleButton = function(btnData) {

        if(btnData.label === 'On'){
            btnData.label = "Off";
        }else{
            btnData.label = "On";
        }
};

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

exploring numerous boxes in an engaging and educational tutorial

Explaining this in the title was a bit tricky, but what I want to create is an interactive tutorial. In this tutorial, the user will click on an area or product they want to clean or use for cleaning. After clicking, another box with relevant information w ...

Unable to execute $http in AngularJS Plunker

Having trouble running $http on the Plunker. Can you please review my code and assist me? var QuizApp = angular.module('QuizApp', []); QuizApp.controller('QuizController', ['$scope','$http',function($scope,$http) { ...

Tips on updating content at a specific time without the need to refresh the page

I'm working on a digital signage script that needs to be time scheduled. I was able to accomplish this using JavaScript and refreshing the page every 15 minutes. However, my question is, how can I track the time and update the content at specific hour ...

Transferring information from user input to a table using AngularJS

I need assistance with AngularJS to transfer data from 2 input fields (name and last name) into a table row. <input type="text" ng-model="name"> <input type="text" ng-model="lastname"> Here is the structure of my table: <tabl ...

Maintain the values of radio buttons, dropdowns, and checkboxes using Javascript

When I click on a radio button, it activates a drop down menu. Upon selecting different values from the drop down, various checkboxes become visible. Now, I want to preserve the selection of the radio button along with the selected drop down values and ch ...

Developing Angular dynamic components recursively can enhance the flexibility and inter

My goal is to construct a flexible component based on a Config. This component will parse the config recursively and generate the necessary components. However, an issue arises where the ngAfterViewInit() method is only being called twice. @Component({ ...

JavaScript - How can I prevent receiving multiple alerts during long polling success?

After following a video tutorial on implementing long polling, I managed to get it working. However, I encountered an issue where my alert message pops up multiple times even though I only receive one response from the server. I was under the impression th ...

AngularJS - Is there a way to determine the route between two states in an application?

Hello everyone, I am still getting the hang of AngularJS and have a question that's been on my mind: Is there a way for me to track the path of a state from one state to another? I am curious about how I can display all the states that are traversed ...

Tips for Implementing {{ }} within Angular Filters in Nested ng-repeat statements

I am looking to incorporate {{ }} within the Angular "filter" filter, specifically while nested inside an ng-repeat. Let's consider the relationship below: var categories = [ {"title":"land"}, {"title":"sea"}, {"title":"air"} ]; var vehi ...

Explore Marker GeoCharts within AngularJS to enhance visualization capabilities

I am trying to customize the markers on a GeoChart similar to the one shown in this example. Could you please guide me on how to add the mapsApiKey in Google GeoChart using the ng-google-chart directive? Here's an example code snippet: var chart = {} ...

Tips for retrieving a value returned by a Google Maps Geocoder

geocoder.geocode( { 'address': full_address}, function(results, status) { lat = results[0].geometry.location.lat(); lng = results[0].geometry.location.lng(); alert(lat); // displays the latitude value correctly }); alert(lat); // does ...

Tips for creating multiple files using nodejs and express

I am currently working on developing a personalized code editor that consists of 3 textareas: html, css, and javascript. The objective is to save the data from each textarea into individual files. With the help of express and nodejs, I have successfully m ...

Angular prevents the page from reloading when using `$window.location`

I'm facing an issue while trying to refresh my admin page using Angular. I've come across $window.location.reload() and $route.reload(). I attempted to use $window.location.reload() by injecting $window into the function parameters. scope.uploadL ...

Can Google Maps markers be transformed into checkboxes for use?

Is there a way to use Google Maps as a multiple locations picker, where users can select map markers and submit them through a form? ...

Pass data from Wordpress plugin to JavaScript function

I recently developed a plugin for a WordPress website. Inside the plugin, I am trying to call a JavaScript function (passing a string as a parameter) from a file that I have just enqueued. However, nothing seems to be happening. Can anyone help me identify ...

When utilizing JavaScript's split method, an empty string returns an array size of one instead of zero

Is there a way to separate a string using commas to create an array in Node.js? exports.test = function(rq, rs){ var mailList = "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="16657e7764777856627365623875797b">[email ...

Unable to update values in Google Sheets using the node.js API

I have been working on a node.js project that involves extracting the location of a cell based on a person's name and date. While I am able to determine this information easily, I encounter difficulties when trying to update the cell using the .update ...

The server encountered a 405 error due to its inability to redirect to the specified URL

Hi there! I'm currently in the process of developing a payment page using WebAPI2 and AngularJS. Within my payment API, there is a field called "redirecturl" which is used to redirect users to a specific page once the payment is successful. I've ...

Enhance text area size using AngularJS

Looking to dynamically adjust the number of rows in a text area on focus and blur events, as well as show a hidden div layer upon focus. Currently achieving this with the following code: <textarea class="form-control" rows="1" placeholder="Please enter ...

Advantages of using index.js within a component directory

It seems to be a common practice to have an index file in the component/container/module folders of react or angular2 projects. Examples of this can be seen in: angular2-webpack-starter react-boilerplate What advantages does this bring? When is it recom ...