加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – 如何分配布尔值到模型

发布时间:2020-12-17 08:44:47 所属栏目:安全 来源:网络整理
导读:我有两个输入单选按钮的以下视图: label input type="radio" name="test" ng-model="fooBar" value="true" ng-change="logConsole()" True/labellabel input type="radio" name="test" ng-model="fooBar" value="false" ng-change="logConsole()" False/lab
我有两个输入单选按钮的以下视图:
<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>

我的控制器看起来像:

//Initialization
$scope.fooBar = false;

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

我的问题是,当用户选择单选按钮,fooBar模型的类型总是一个字符串,这是值是字符串’真’或字符串’假’ – 我想要的类型是一个布尔值true或布尔false值。
如何存储一个布尔值(从视图内)到模型?

编辑:我只是试过这个,它仍然不工作。对于value属性,我传递了一个函数,它将返回boolean true或false,如下所示:

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

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

当选择单选按钮时,仍然会产生字符串…

the documentation的意见之一说:

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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读