Angular model is not receiving the value of a false checkbox

One issue I am facing is with a form that includes a checkbox bound to an ng-model property. The problem arises when the checkbox is checked, as the value gets set in the model. However, when it is unchecked, the key and value remain unset, causing API validation errors.

<div class="form-group">
    <label for="title">Title</label>
    <input type="text" ng-model="post.title" placeholder="Post title..." />
</div>
<div class="form-group">
    <label for="author">Author</label>
    <input type="text" ng-model="post.author" placeholder="Your name" />
</div>
<div class="form-group">
    <label for="content">Content</label>
    <textarea ng-model="post.content"></textarea>
</div>
<div class="form-group">
    <label for="visible">Visible?</label>
    <input type="checkbox" ng-model="post.is_visible" />
</div>
<div class="form-group">
    <input type="submit" class="btn btn-primary" value="Save" />
</div>


blogModule.controller('PostCreateController', ['$scope', '$stateParams', 'PostResource',
    function ($scope, $stateParams, PostResource) {
        $scope.post = new PostResource();
        $scope.addPost = function () {
            console.log($scope.post); // post.is_visible is undefined
            //$scope.post.$save();
        }
    }
]);

Prior to saving, the model appears like this:

{
    $resolved: true
    author: "asdag"
    content: "adfadsf"
    title: "adgadf"
    proto: ...
}

I'm puzzled why post.is_visible remains undefined instead of being automatically set to false when not checked. What steps can I take to ensure it's always set to false?

Answer №1

give it a shot

 <input type="checkbox" ng-model="post.is_visible"  ng-init="post.is_visible=false" />

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

Optimizing AngularJS performance with REST service caching

My AngularJS application requires caching of REST service responses, and I've come across libraries like angular-cached-resource that store data in the local browser storage. However, there are times when certain cached responses need to be deleted d ...

Can the issue of mangling be avoided during .NET minification?

Currently, I am utilizing the .NET Bundling and Minification feature to bundle and minify the files for my application. While this method works well, I am interested in exploring alternative solutions due to its complexity. Specifically, I am attempting t ...

Utilizing Contentful along with Nuxt.js causes the error message "Required accessToken parameter missing"

I created a webpage that pulls information from Contentful. Although the data is being retrieved correctly, buttons utilizing functions from methods are not working. Additionally, real-time updating of variables (such as using v-model) is not functioning. ...

Trouble with the display:none attribute in Firefox and Chrome

<tr style="height:5px" id="TRRSHeaderTrialBar" name="TRRSHeaderTrialBar" style='display:none'> <tr id="TREmail" name="TREmail" style="height:1px;" nowrap style='display:none'> Using the code snippet above to hide the bar w ...

What is the appropriate way to utilize `render_template` from Flask within Angular?

I'm facing an issue where I need to implement different routes for various Angular Material tabs. I have attempted to directly call Flask from the template, as demonstrated below, but unfortunately, I am unable to invoke render_template from Angular. ...

Starting over fresh after clearing the interval

Visit the test site Is there a way to reset the animation back to the beginning once it reaches the last image? I've attempted the code below, but it doesn't seem to bring it back to the first image. $(document).ready(function () { window. ...

Is there a way to access the current $sce from a controller?

One way to access the current $scope outside of a controller is by using the following code: var $scope = angular.element('[ng-controller=ProductCtrl]').scope(); Is there a way to retrieve the $sce of the current controller? ...

Methods for adding a new object to an array in Angular: Explained

How can I insert a new object in Angular? Here is the current data: data = [ { title: 'Book1' }, { title: 'Book2' }, { title: 'Book3' }, { title: 'Book4' } ] I would like to update the obje ...

Is the security of Angular's REST authentication reliable?

My goal is to establish a secure connection with a REST service using Angular. I have come across the official method, which involves setting the authentication ticket like this: $httpProvider.defaults.headers.common['Authorization'] = 'dhf ...

Utilizing the "return" keyword in Javascript outside of function declarations

Exploring the Impact of Using the Return Keyword in JavaScript Scripts Beyond Functions in Browsers and Node.js Recently, I experimented with utilizing the return keyword in a Node.js script like so: #!/usr/bin/env node return 10; My initial assumption ...

A few of the npm packages that have been installed globally are not functioning properly

After installing npm globally, I checked its version using npm -v and it displayed correctly as 7.13.0. Similarly, I installed heroku-cli globally, but when I ran heroku --version, it returned the error message: C:\Users\MyName\AppData&bso ...

Updating values in mongoDB using Express.js and axios: A step-by-step guide

I need help figuring out how to update a specific post's data in my mongoDB using its object id. I have created an HTML form that displays the selected post's data and allows me to make changes, then submit the updated data to http://localhost:50 ...

Utilizing Zustand state management with Next.js 13.4.12 for routing and server-side rendering, enhanced with local storage

My Zustand store code looks like this: import create from "zustand"; import { persist } from "zustand/middleware"; const useProjectStore = create( persist( (set) => ({ selectedProject: null, setSelectedProject: (pr ...

angular array with multiple dimensions

I'm working with Angular and have a JSON string passed to it. Within the array fields, there is another array called products. I'm trying to figure out how to iterate over these products using an Angular repeat. Simply using {{item.products}} pr ...

Display a specific tab section upon clicking using jQuery or JavaScript

Hello everyone! I am completely new to JavaScript. I have added a tab slider to my HTML with 3 categories in the tab menu: All, Creative, and Branding. How can I show a div after clicking on one of the list items? I have assigned classes to the list items ...

Creating a loading indicator in Angular using ui-router to display when transitioning between states

Can I display a loading icon in Angular when switching to another state using ui-sref until the second state is fully loaded? ...

Guide: Storing data from radio type AngularJS form into MongoDB using MEAN Stack

Feeling frustrated! I'm dealing with a complex multistep form that gathers information based on user selections. The form consists of radio type choices, except for the "name" field which accepts text input. To handle this form, I have implemented An ...

Tips for preventing the playback of the sound while recording

When creating a basic JavaScript application that involves opening a stream from the user and analyzing frequencies, I found that while Google Chrome and Opera work well by providing feedback in the headphones, Firefox often remains silent. Additionally, F ...

``There seems to be an issue with setting the input value attribute using jQuery's .html

I've been trying to update the value attribute in the input element within my HTML code, but so far, I haven't had any luck with it. HTML: <div class='photo-info'> Photo Name : <span class='photo-name'><?p ...

ng-repeat dysregulating the binding of directive models

<input-directive model="config.shared.product.whatevers[0]"></input-directive> <!-- This code is functioning correctly, however the following does not bind properly --> <td ng-repeat="whatever in config.shared.product.whatevers trac ...