Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to assign Boolean value to model

Tags:

angularjs

I have the following view with two input radio buttons:

<label>     <input type="radio" name="test" ng-model="fooBar"          value="true" ng-change="logConsole()">     True </label>  <label>     <input type="radio" name="test" ng-model="fooBar"          value="false" ng-change="logConsole()">     False </label> 

And my controller looks like:

//Initialization $scope.fooBar = false;  $scope.logConsole = function () {     console.log("Value is : " + $scope.fooBar);     console.log("Type is : " + typeof $scope.fooBar); //Always displays 'string' }; 

My problem is that when the user selects either of the radio buttons, the type of the fooBar model is always a String, that is the value is either the String 'true' or the String 'false' - I want the type to be a boolean true value or a boolean false value. How can I store a boolean value (from within the view) onto the model?

EDIT: I just tried this out and it still does not work. For the value attribute, I passed a function that would return boolean true or false, something like this:

<input type="text" value="{{getBoolean('true')}}....> 

$scope.getBoolean = function (value) { if (value === 'true') return true; else return false; };

It still results in a String when the radio buttons are selected...

like image 414
callmekatootie Avatar asked Jul 17 '13 14:07

callmekatootie


People also ask

How do you assign a value to a Boolean?

To declare a Boolean variable, we use the keyword bool. To initialize or assign a true or false value to a Boolean variable, we use the keywords true and false. Boolean values are not actually stored in Boolean variables as the words “true” or “false”.

How do you assign a boolean value in Python?

If you want to define a boolean in Python, you can simply assign a True or False value or even an expression that ultimately evaluates to one of these values. You can check the type of the variable by using the built-in type function in Python.

How do you assign a boolean value in Java?

In Java, there is a variable type for Boolean values: boolean user = true; So instead of typing int or double or string, you just type boolean (with a lower case "b"). After the name of you variable, you can assign a value of either true or false.

How do you create a Boolean variable?

One of them is ' bool . ' The ' bool ' type can store only two values: true or false . To create a variable of type bool, do the same thing you did with int or string . First write the type name, ' bool ,' then the variable name and then, probably, the initial value of the variable.

How to get the value of a Boolean in Vue?

Here is a simple solution. Alternatively, you can use v-bind. v-binded "true"/"false" will be real boolean true / false. new Vue({ ... data() { return { foo: false, // this will be boolean true or false, rather than "true" or "false".

What is the Boolean value of an expression?

The Boolean value of an expression is the basis for all JavaScript comparisons and conditions. The Boolean value of -0 (minus zero) is false: The Boolean value of "" (empty string) is false: The Boolean value of false is (you guessed it) false:

What is the Boolean value of false in JavaScript?

The Boolean value of "" (empty string) is false: The Boolean value of false is (you guessed it) false: Do not create Boolean objects. The new keyword complicates the code and slows down execution speed. Note the difference between (x==y) and (x===y). (x == y) true of false? (x === y) true of false?

Is there a Boolean modifier for V-model values?

Sign in to your account What problem does this feature solve? There is a .number modifier for v-model which automatically parses the value as integer. It would be useful to have a similar modifier for boolean values.


1 Answers

One of the comments in the documentation says:

While ng-value is not documented, it is a useful directive, specifically when you are binding to a boolean value.

<input ng-model="foo" type="radio" value="true">  

may not work but

<input ng-model="foo" ng-value="true" type="radio">  

does.

like image 145
JB Nizet Avatar answered Oct 13 '22 11:10

JB Nizet