AngularJS: ng-checked not truly linked

When ng-checked is true, the checkbox in my HTML should also be checked. It seems like this only happens during initialization and not after.

I am noticing a discrepancy between ng-checked and what is displayed in the browser.

var app = angular.module('MyApp',[]);
    
app.controller("MyCtrl", function($scope) {

  $scope.testCheckedState = true;

  $scope.isTestChecked = function(){
    return $scope.testCheckedState;
  };

  $scope.forceCheck = function(){
    $scope.testCheckedState = true;
  };

});
<!DOCTYPE html>
<html>

  <head>
    <script data-require="angular.js@*" data-semver="1.3.8" src="https://code.angularjs.org/1.3.8/angular.js"></script>
  </head>

  <body ng-app="MyApp">
    <div ng-controller="MyCtrl">
      <input type="checkbox" ng-checked="isTestChecked()" ng-click="forceCheck()" />
      <br />
      isTestChecked: {{isTestChecked()}}
    </div>
  </body>

</html>

I am confused by what could be causing this difference in states?

Thank you, RoD

Answer №1

<input type="checkbox" ng-model="testCheckedState" />

Sincerely

Answer №2

<input type="checkbox" ng-checked="isChecked()" ng-click="toggleCheck()" />

ngChecked enables bi-directional data binding with a function expression. It is crucial for the expression to utilize a $scope variable, as shown in this example.

<div ng-init="checked = false">
   <input type="checkbox" ng-model="checked" />
   <input type="checkbox" ng-checked="checked"/>
</div>

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

"Discover the best way to sort through an array of complex objects with varying levels of nesting using a specified search

I have come across similar answers, but none of them go beyond two levels deep. Therefore, I believe this is not a duplicate problem. My task is to filter an array of objects where each object may contain nested objects of unknown depth. In my data structu ...

The on-screen keyboard using JQuery and CSS is not working properly on an HTML redesign

UPDATE - Check out the modified version here: https://codepen.io/anon/pen/wZQjQy Here is the original for reference: https://codepen.io/anon/pen/YMRLBN (had to utilize CodePen, as simply adding a snippet here doesn't link the CSS) I essentially copie ...

When the page reloads, the firebase currentUser variable is found to be

Currently utilizing Firebase for authentication. Prior to making a request to the backend, I always ensure to request the idToken. service.interceptors.request.use(async request => { const token = await firebase.auth().currentUser.getIdToken(); i ...

What is the process behind __doPostback generating a URL on an ASP.NET website?

Can you please check out the following URL: When you select "Exhibitor A to Z" from the dropdown menu and click search, a javascript function is triggered for each link (javascript:__doPostBack('ctl00$MainContent$grdExhList$ctl00$ctl04$lnkExhLink&ap ...

After being redrawn, the line on the canvas vanishes

After updating the angle value, the line is quickly redrawn correctly but then disappears. It seems like the input value may be getting refreshed and set to undefined again. How can I ensure that the line retains the correct angle? <script language=" ...

HTML5 input type Color displays individual RGB values

This code snippet opens up a variety of options for the user to manipulate different color values such as R, G, B, HEX VALUE, HUE, etc. However, the specific requirement is to only extract the Red value. <input id="color_pick"type="color" value="#ff0 ...

Using AppCtrl's variable in another controller

Consider the following HTML code snippet: <html ng-app ng-controller="AppCtrl"> <head> <script src="http://code.angularjs.org/1.0.6/angular.min.js"></script> <script src="script.js"></script> </head ...

Presenting data fetched from Firebase database in an HTML table format

I recently started working with Firebase and I'm attempting to fetch static data from my Firebase Database to showcase in a tabular format on an HTML page. Below is the structure of the HTML code I've implemented: <section> <table> ...

Angular4 and jQuery working together for effective navigation and pagination

Trying to implement pagination in angular 4 and jQuery, but encountering an issue where clicking on the next button causes it to skip through multiple pages at once (2, then 3, then 5)... Component code: addClass(x) { $(".page-item").click(function () ...

Ways to invoke foo specifically when either a click event or a focus event is triggered

In my custom directive, I am binding focus and click events to an element: app.directive('mydirective', function () { return { link: function ($scope, $element, $attrs) { $element.bind('click focus', function (e) { f ...

Retrieving Information from JSON File Using a Variable (JavaScript/Discord.js)

While I was coding my Discord bot, I encountered an unexpected issue. Normally, after linking a JSON file, you can access data by using jsonFile.path to retrieve specific information. However, I faced a challenge where I needed to replace the path with a ...

Adding a static javascript file to a Vue CLI 3 project is a straightforward process

Currently working on a vue-cli-3 project and looking to include a static javascript file. Curious about the proper way to import this file. The typical method used is: <script src="js/myfile.js"></scrip> ...

Changing the nested state using a Redux reducer

I've come across a perplexing issue that is really baffling me. I'm working on developing my web application using React and Redux, where I have implemented a notification system with Firebase. Each notification follows this structure: var no ...

It is possible to nest an 'ng-view' inside another 'ng-view

Is it possible to nest ng-views in AngularJS or is there an alternative solution for achieving this? I'm curious if we can have an ng-view inside another ng-view. ...

Is it possible to display tooltips in amcharts based on specific conditions?

How can I display tooltip text in the chart based on a specific condition? I only want to show the tooltip text if the value is not equal to 0. Series.columns.template.tooltipText = `{valueY}s`; The tooltip currently displays the value of {valueY}, but I ...

The children prop in React Typescript is specified in the props type, but for some reason it is not being

I am currently developing a component library using a combination of React, TypeScript, Styled Components, and Rollup. One of the components I have created is a Button component, defined using a type interface. After rolling up the library and importing th ...

sending a collection of image data arrays wrapped in FormField objects from Angular to Express

I am facing a challenge while trying to upload two FormField objects along with form data to express. I am having trouble using the multer library in express to extract this data from the request. While I can access the form data, the FormField objects re ...

Utilizing a plugin to execute a function in Wordpress

I'm currently facing the challenge of combining two WordPress plugins without the need to modify one to fit into the other seamlessly. My main question is: How can I call a function from one plugin that exists outside of another plugin? For example, ...

How to eliminate a hyperlink from an HTML element with the help of JQuery

Recently, I was assigned to revamp a website for the company I work for. However, upon closer inspection, I realized that the website is quite messy and relies heavily on templates, resulting in certain elements being auto-generated as active links. The i ...

I'm wondering how I can design a utility function within my Redux module that can extract a specific subset of read-only data from the current state

I am currently utilizing redux to create a "helper function" inside my redux module that is responsible for fetching filtered data from the state based on a specified index. This specific data will be used to generate a form consisting of inputs depending ...