zoukankan      html  css  js  c++  java
  • angular如何实现input checkbox / radio 的双向数据绑定

     1 <!DOCTYPE html>
     2 <html >
     3 <head>
     4     <meta charset="UTF-8">
     5     <title>radio & checkbox</title>
     6     <script src="js/angular.js"></script>
     7 </head>
     8 <body ng-app="myApp" ng-controller="myCtrl">
     9     <input type="radio" name="sex" value="male" ng-model="person.sex" />10     <input type="radio" name="sex" value="female" ng-model="person.sex" />11     <input type="text" ng-model="person.sex" />
    12 
    13     <input type="checkbox" ng-true-value="true" ng-false-value="false" ng-model="person.like.pingpong" />乒乓球
    14     <input type="checkbox" ng-true-value="true" ng-false-value="false" ng-model="person.like.football" />足球
    15     <input type="checkbox" ng-true-value="true" ng-false-value="false" ng-model="person.like.basketball" />篮球
    16     <span>{{ person.like.pingpong }} {{ person.like.football }} {{ person.like.basketball }} </span>
    17     <br/>
    18     <input type="checkbox" ng-true-value="true" ng-false-value="false" ng-model="btnGroup.audioBtn" ng-click="audiofun()"/>声音按钮
    19     <input type="checkbox" ng-true-value="true" ng-false-value="false" ng-model="btnGroup.alarmBtn" ng-click="alarmfun()"/>报警按钮
    20     <span>声音按钮{{ btnGroup.audioBtn }} 报警按钮{{ btnGroup.alarmBtn }}</span>
    21 </body>
    22 </html>
    23 
    24 <script type="text/javascript">
    25     var app = angular.module('myApp', []);
    26     app.run(function($rootScope) {
    27         $rootScope.person = {
    28             sex: "female",
    29             like: {
    30                 pingpong: true,
    31                 football: true,
    32                 basketball: false
    33             }
    34         };
    35     })
    36     .controller('myCtrl',function($scope){
    37         $scope.btnGroup={audioBtn:true,alarmBtn:true};
    38         $scope.audiofun=function(){
    39           if($scope.btnGroup.audioBtn)
    40           {
    41              console.log($scope.btnGroup.audioBtn); 
    42           }else{
    43              console.log($scope.btnGroup.audioBtn); 
    44           }
    45         }
    46         $scope.alarmfun=function(){
    47           if($scope.btnGroup.alarmBtn)
    48           {
    49              console.log($scope.btnGroup.alarmBtn); 
    50           }else{
    51              console.log($scope.btnGroup.alarmBtn); 
    52           }
    53         }
    54 
    55     })
    56 </script>

     效果:

  • 相关阅读:
    final关键字
    海思NB-IOT的SDK看门狗的使用
    IAR环境下编译CC2640入门开发
    股票操作记录180613(2)
    股票操作笔记18年6月13(1)
    PyYAML学习第一篇
    片仔癀犯过的错误
    2018年5月份片仔癀最佳演员奖
    2018-05-22两只垃圾基金南方产业活力000955和鹏华全球高收益债券000290
    linux c编程:网络编程
  • 原文地址:https://www.cnblogs.com/i-want-to-be-like-an-sun/p/7687393.html
Copyright © 2011-2022 走看看