Model is updated by checkbox only on the second click

Take a look at this Plunkr example here: http://plnkr.co/edit/hwVL3xtnD9hGJL?p=preview

After clicking the checkbox for the first time, the model fails to update. Can you explain why?

var app = angular.module('main', ['ngMaterial'])
    .controller('DemoCtrl', function($scope, $filter, $http) {
      $scope.propdata = [];
      $scope.success ="loading..";
      var url = "api.json";
       $http({url:url,
         method:"GET"
       }).then(function (rs){
         $scope.propdata = rs.data[0];
         $scope.success ="done..";
       });
    });

Answer №1

The JSON data you are receiving contains a value of 1, rather than true or false. If you update the data to use true or false, it will correctly interpret the initial value. Alternatively, you can convert the "1" to a boolean before assigning it.

Additional Note:

An alternative approach is to specify the ng-true-value and ng-false-value attributes on your checkbox so that it can handle the 0 and 1 values that you have. The single quotes surrounding "'1'" indicate that it should be treated as a string. See the example below:

HTML

<div class="checkbox {{font_size}}">
     <label for="garden_service">
          <input type="checkbox" id="garden_service"
                      ng-checked="propdata.garden_service==1" ng-true-value="'1'" ng-false-value="'0'"
                       ng-model="propdata.garden_service">Garden Service <br/>model value:{{propdata.garden_service}}
     </label>
</div>

http://plnkr.co/edit/t280UjC3NYNtQt28W1wm?p=preview

Official documentation link: https://docs.angularjs.org/api/ng/input/input%5Bcheckbox%5D

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

Discovering common elements in various arrays of objects

Details: record1 = [{"site": "The Blue Tiger", "zipcode": "E1 6QE"}, {"site": "Cafe Deluxe", "zipcode": "E6 5FD"}] record2 = [{"site": "Blue Tiger", "zi ...

My node.js and express application is encountering an issue where it is failing with an error indicating that a variable has not been

Currently in the process of understanding how node.js and express operate. I've successfully extracted data from my mongo database... and now I'm experimenting with transferring data between my router code and views. The locations.js file within ...

CAUTION: Attempted to load angular multiple times while loading the page

I encountered a warning message while working on my project, causing errors in calling backend APIs due to duplicate calls. Despite attempting previously suggested solutions from the forum, I am stuck and seeking assistance. Can anyone provide guidance? Be ...

The size of Bootstrap 3 columns adjusts dynamically based on the width of the window as the page is being

I am facing an issue with the layout of my bootstrap site when resizing the 3 horizontal columns based on the window size upon page load. Currently, I have a script that adjusts the height of each column to match the tallest one so they appear uniform whe ...

Even though setState is supposed to update the state and trigger a render, it's not showing up in the view for some

My React app consists of a simple word/definition feature. There is an edit box that appears for users to change the definition when they click on "edit". Even though I call getGlossary() to update the new definition in the state, I can see the changes in ...

Extracting names from HTML can be done easily by looking for the "@" symbol that the

My HTML structure looks like this: <table id="table-15244" cellspacing="0" class="comment"><tbody> <tr id="comment-37"> <td style="color: #999" class="NV"> </td> <td class="hidden-mob"> ...

Determine if an HTML element contains a specific class using JavaScript

Is there a simple method to determine if an HTML element possesses a particular class? For instance: var item = document.getElementById('something'); if (item.classList.contains('car')) Remember, an element can have more than one clas ...

When attempting to reference a custom module within a React application, the error message "moment is not a function" may appear

I am facing an issue while trying to integrate a custom module I developed into a basic React application using react-boilerplate. Upon importing the module, I encountered an error stating that the moment function within the module is undefined. Here is t ...

What is the correct way to pass parameters when using the setState() function in React hooks?

I am working on a project where I have a list of country names. When a user clicks on one of the countries, I want to update the state with the name of the newly selected country. This state change will then trigger other changes in a useEffect(). The stat ...

Combining inheritance and isolated scopes: a comprehensive guide

I've encountered a situation where I need to sort an HTML table. Here is the code snippet: <table> <thead> <tr> <th custom-sort-one order="'Name'" >Name</th> </ ...

How can you modify a hyperlink URL before it is activated?

I'm facing an issue with a URL structure that needs updating before it is clicked. The current URL looks like this: https://url.com/a b c which is causing redirection problems because the actual link has underscores instead of spaces in its URL. Is ...

Unable to remove the specified row from the AngularJS table

I'm experiencing an issue with a table where I dynamically insert rows with edit and delete links for each row. The edit functionality is working fine, but when it comes to deleting a row, it always deletes the first one. Below is the code snippet: ...

Storing large data tables in C#: Tips for efficient caching

Currently, I am facing the challenge of executing a lengthy PL/SQL query and storing the result set in order to serve UI requests. Additionally, I need to refresh this data automatically or manually after some time. The complexity arises from handling mult ...

The display message in Javascript after replacing a specific string with a variable

I'm currently working on a task that involves extracting numbers from a text input, specifically the first section after splitting it. To test this functionality, I want to display the result using an alert. The text input provided is expected to be ...

Attach a child element to the bottom of its parent div when the parent div reaches the bottom

I'm looking to make a child div stick to the bottom when the parent div reaches the bottom of the browser window. Note: This behavior should only occur when the parent div is pushed down, not when the page is scrolled down. For instance, in my demon ...

What is the reason for the Circle to Polygon node module producing an oval or ellipse shape instead of a circle shape?

I've been experimenting with the npm package circle-to-polygon and I crafted the following code to generate a polygon that resembles a circle. const circleToPolygon = require('circle-to-polygon'); let coordinates = [28.612484207825005, 77. ...

What is the best way to pass a value from one directive to another in AngularJS?

Here is the code snippet from my index.html file: <body ng-controller="MainCtrl"> <search myname="myval" change-client='client()'></search> <pagination></pagination> </body> </html> and app.js conta ...

jQuery does not pass data to bootstrap modal

I am currently working with the full calendar feature. Within this framework, I have implemented a modal that allows users to insert new events: <div id="fullCalModal_add_appointment" class="modal fade"> <div class="modal-dialog"> ...

Hey there! I'm currently facing some difficulties with storing form input data into a nested json file

I have developed a Next.js application with a form component structured as follows: components/Form.js import { useState } from 'react' import { useRouter } from 'next/router' import { mutate } from 'swr' const Form = ({ for ...

Is there a method to access the output of getStaticProps function from NextJS API routes?

Is there a method to compute and cache new data during build time that is essential for both the front-end and back-end API routes? I'm looking for a way to access the static properties generated by API routes at build time since the routes are access ...