Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS - Give checkbox an integer?

Tags:

angularjs

I was able to see true or false and return strings in checkboxes with ng-true-value and ng-false-value, but in additional, I want to give the checkbox an integer and if it's checked, it returns an integer.

Which should be similar to when I get a value from a selectbox: http://jsfiddle.net/PuDRm/

But I can't seem to get the same idea in a checkbox: http://jsfiddle.net/PuDRm/1/

<input type="checkbox" ng-model="testc" ng-true-value="Yes" ng-false-value="No" ng-change="processtest()" /> Yes

I'm a total newbie and trying to understand AngularJS.

like image 823
pau Avatar asked Oct 15 '13 00:10

pau


People also ask

How to set checkbox value checked in AngularJS?

AngularJS ng-checked DirectiveThe ng-checked directive sets the checked attribute of a checkbox or a radiobutton. The checkbox, or radiobutton, will be checked if the expression inside the ng-checked attribute returns true. The ng-checked directive is necessary to be able to shift the value between true and false .

How do you pass checkbox value 0 if not checked and 1 if checked angular?

How do you pass checkbox value 0 if not checked and 1 if checked? Add a hidden input that has the same name as the checkbox with the value of 0 BEFORE the checkbox. If the checkbox is unchecked, the hidden field value will be used, if it is checked the checkbox value will be used.

How to get checked and unchecked checkbox value in AngularJS?

The ng-checked Directive in AngularJS is used to read the checked or unchecked state of the checkbox or radio button to true or false. If the expression inside the ng-checked attribute returns true then the checkbox/radio button will be checked otherwise it will be unchecked.

How to set checkbox checked on button click in AngularJS?

Approach: The approach is to use the ng-checked directive to check the checkbox in the DOM. In the first example, a single checkbox is checked by the button and In the second example, multiple checkboxes are checked by the button. The ng-model directive is used to bind the checkboxes.


3 Answers

It sounds like you are trying to bind to two separate values with the checkbox.

In the select the view is the name you gave e.g. 'test1', and the model is the number, e.g. '1'. For the checkbox, the view is a check or a blank square and the model is Yes/No (or whatever you want to put in the ng-true-value and ng-false-value). There's no room to have it produce a number too.

It would be simpler to let the checkbox model be the default (boolean) type by omitting the ng-true-value and ng-false-value attributes. You can then display the number and text based on this like in this updated fiddle: http://jsfiddle.net/D5DGf/1/

The {{ ... }} bindings take care of updating when the function values change, so you don't need to use ng-change either.


What was wrong with the original checkbox fiddle?

In the processtest function you are taking the model (Yes/No) and changing it to always be either 8 or 1. The checkbox (in fact, ngModel) doesn't understand this as it expects to always see Yes/No. As it doesn't know what to do, it falls back to being unchecked.

This is why the checkbox in the original fiddle is uncheckable.

like image 70
Andyrooger Avatar answered Nov 12 '22 09:11

Andyrooger


If you still want to set use a number for your model and checbox value, you could do it with a custom directive to parse the string back to integer.

Demo: http://jsfiddle.net/qw5zS/

html

<input type="checkbox" ng-model="test" parse-int ng-true-value="1" ng-false-value="0" /> 

js

app.directive('parseInt', [function () {
    return {
        restrict: 'A',
        require: 'ngModel',
        link: function (scope, elem, attrs, controller) {
            controller.$formatters.push(function (modelValue) {
                console.log('model', modelValue, typeof modelValue);
                return '' + modelValue;
            });

            controller.$parsers.push(function (viewValue) {
                console.log('view', viewValue, typeof viewValue);
                return parseInt(viewValue,10);
            });
        }
    }
} ])

But this might not be a good practice using checkbox, you should stay with the default behavior and use a boolean, then format the view like you want, but keep your data clean.

like image 9
jpmorin Avatar answered Nov 12 '22 09:11

jpmorin


If you change the column in database from integer to char, it works with

<input type="checkbox" ng-model="testc" ng-true-value="1" ng-false-value="0" ng-change="processtest()" /> Yes
like image 6
Mihai Pop Avatar answered Nov 12 '22 09:11

Mihai Pop