25458

Binding checkboxes to object values in AngularJs

The problem is in binding the state of the checkbox (checked/unchecked) to the object values.

HTML:

<div ng:controller="Ctrl"> <div ng:repeat="(letter, number) in obj"> {{letter}} and {{number}} <input type="checkbox" ng:model="obj[letter]"> </div>

Controller:

function Ctrl() { this.obj = {a:true,b:true}; };​

When the first checkbox is clicked it affects the state of the second checkbox, but the model is correct, so obj becomes {a:false, b:true}.

Example can be found at http://jsfiddle.net/alexpetrov/tRxzr/

How to fix this?

Answer1:

Bind ng-repeat to objects rather than primitive types.

function Ctrl() { this.obj = [{id: 'a', checked: true}, {id: 'b', checked: true}]; }

http://jsfiddle.net/tRxzr/1/

Binding to primitive types confuses ng-repeat, it's a bug: https://github.com/angular/angular.js/issues/933

Answer2:

When the JSON is not entirely in your control, you get a primitive array instead of an object. You want to do an ng-repeat on the same.

To bind ng-repeats to checkboxes to a primitive array and get the selected items. See the plunker code here.

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

Recommend

  • How to trigger a element using jquery
  • Trouble with Azure IoT C# SDK Certificate
  • IBM Connections API - Uploading files in a community and getting nonce - Widget
  • What happens when android app is force closed
  • converting an object to float in pandas along with replacing a $ sign
  • Add spaces between words in spaceless string
  • Messed up characters in webpages (especially social media)
  • List comprehension with if conditional to get list of files of a specific type
  • How to load gif image while ajax content is loading and javascript [duplicate]
  • CakePHP ACL tutorial initDB function warnings
  • Using a canvas object in a thread to do simple animations - Java
  • MonoTouch: How to download pdf incrementally as indicated in the Apple slides “Building Newsstand Ap
  • Sencha Touch 2.0 Controller refs attribute not working?
  • C++ Partial template specialization - design simplification
  • The plugin 'org.apache.maven.plugins:maven-jboss-as-plugin' does not exist or no valid ver
  • FFmpeg Conversion Error
  • Is there a javascript serializer for JSON.Net?
  • Java Scanner input dilemma. Automatically inputs without allowing user to type
  • Where to put my custom functions in Wordpress?
  • what is the difference between the asp.net mvc application and asp.net web application
  • Return words with double consecutive letters
  • bootstrap to use multiple ng-app
  • VB.net deserialize, JSON Conversion from type 'Dictionary(Of String,Object)' to type '
  • Matrix multiplication with MKL
  • Buffer size for converting unsigned long to string
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • How do I configure my settings file to work with unit tests?
  • How to stop GridView from loading again when I press back button?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Bitwise OR returns boolean when one of operands is nil
  • sending mail using smtp is too slow
  • MATLAB: Piecewise function in curve fitting toolbox using fittype
  • Running Map reduces the dimensions of the matrices
  • costura.fody for a dll that references another dll
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Reading document lines to the user (python)
  • How can I use `wmic` in a Windows PE script?
  • Net Present Value in Excel for Grouped Recurring CF
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?