zoukankan      html  css  js  c++  java
  • ionic -- 单选框使用数据双向绑定(angularjs)问题

    ionic -- 单选框使用数据双向绑定(angularjs)问题

    今天在项目用需要使用<input>标签的type=radio 的双向绑定功能,一开始ng-model绑定的变量在controller里一直无法实现绑定功能;源码如下:

    在html里的代码:

    [html] view plain copy
     
    1. <label for="group1"><input type="radio" id="group1" class="group"  name="group" value="1" ng-model="choice" ><span>公开</span></label><br>  
    2. <label for="group2"><input type="radio" id="group2" class="group" name="group" value="2" ng-model="choice" ><span>好友可见</span></label>  


    controller里代码:

    [html] view plain copy
     
    1. // 初始化单选框的值  
    2.     $scope.choice = 1;  
    3.     // 点击事件,获取被选中的单选框的value值  
    4.     $scope.send = function(){  
    5.       console.log($scope.authority.choice);  // 一直为初始化的值:1  
    6.     };  


    不管我怎么获取,都获取不到单选框的值,后台发现原来是我绑定值的时候出了问题:

    正确代码

    [html] view plain copy
     
    1. <label for="group1"><input type="radio" id="group1" class="group"  name="group" value="1" ng-model="authority.choice" ><span>公开</span></label><br>  
    2. <label for="group2"><input type="radio" id="group2" class="group" name="group" value="2" ng-model="authority.choice" ><span>好友可见</span></label>  

    controller:

    [html] view plain copy
     
    1. // 初始化单选框的值,并放到一个对象里面  
    2.     $scope.authority= {"choice" :1};  
    3.     // 点击事件,获取被选中的单选框的value值  
    4.     $scope.send = function(){  
    5.       console.log($scope.authority.choice);  // 一直为初始化的值:1  
    6.     };  




    ** ng-model = 对象属性

        $scope.authority = {"choice":1}  必须以对象形式初始化值;

    希望对大家能有帮助,不要像我一样在这个坑里花费这么长时间。。。

  • 相关阅读:
    pat甲级1013
    二分查找
    pat甲级1012
    win10 + Ubuntu16.04双系统安装
    win10 U盘重装
    win10蓝牙添加设备无法连接
    Android自定义控件总结
    11.粘性控件
    10.侧拉删除
    9.视差特效、回弹动画、overScrollBy
  • 原文地址:https://www.cnblogs.com/callmeguxi/p/8269489.html
Copyright © 2011-2022 走看看