选择框中的Angular

选择框中的Angular

本文介绍了选择框中的Angular,boolean值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用select将布尔值设置为true或false这是我的代码:

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>

值(proposal.formalStoryboard)正确设置为true或false 但更改为当值已经分配时,没有反映在选择框上。

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.

我试过ng-value =true和ng-value =false代替只是价值,但它不能正常工作。

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

推荐答案

编辑:评论员指出我的原始解决方案没有像声称的那样工作我已经更新了答案以反映下面其他人给出的正确答案(我无法删除已接受的答案)。

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).

对于Angular 1.0.6,请考虑以下HTML:

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>

这个JavaScript:

And this JavaScript:

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

这是一个工作,这是一个适用于Angular的 1.3.14,略有不同。

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.

这篇关于选择框中的Angular,boolean值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 10:08