Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular, boolean value in a select box

I want to set a boolean value to true or false using a select here is my code:

<select class="span9" ng-model="proposal.formalStoryboard"> <option value="false">Not Included</option> <option value="true">Included</option> </select> 

The value (proposal.formalStoryboard) is set properly to true or false but the change are not reflected on the select box when the value is already assigned.

I tried ng-value="true" and ng-value="false" instead of just value but it's not working as well.

like image 999
Jerome Ansia Avatar asked Jul 19 '13 19:07

Jerome Ansia


People also ask

How do you give a Boolean a value?

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 define a boolean variable in TypeScript?

Boolean values are supported by both JavaScript and TypeScript and stored as true/false values. TypeScript Boolean: let isPresent:boolean = true; Note that, the boolean Boolean is different from the lower case boolean type.

What is Boolean in angular?

The boolean is a primitive type in Typescript. It represents a simple true/false value. They are implemented as numerical values with a single binary digit (i.e., 0 & 1). The Boolean is an object wrapper for the primitive boolean value.

What values do Booleans take?

Boolean values and operations There are just two values of type bool: true and false. They are used as the values of expressions that have yes-or-no answers.


1 Answers

EDIT: Commentors have pointed out that my original solution did not work as claimed. I have updated the answer to reflect the correct answer given by others below (I cannot delete an accepted answer).

For Angular 1.0.6, consider this HTML:

<div ng-app="">   <div ng-controller="MyCntrl">     <select ng-model="mybool"             ng-options="o.v as o.n for o in [{ n: 'Not included', v: false }, { n: 'Included', v: true }]">     </select>     <p>         Currently selected: <b>{{ mybool }}</b> opposite: {{ !mybool }}    </p>   </div> </div> 

And this JavaScript:

function MyCntrl($scope) {     $scope.mybool = true; } 

Here is a working DEMO for Angular 1.0.6 and here is a working DEMO for Angular 1.3.14, which is slightly different.

like image 159
ChrisP Avatar answered Sep 23 '22 16:09

ChrisP