AngularJS中在<input type="checkbox" ng-model="ceshi" >与$scope.ceshi 那些事

问题描述:点击checkbox,左侧框中数据全部出现在右侧框或者消失。

直接绑定<input type="checkbox" ng-model="ceshi" > 通过自带指令ng-model绑定ceshi。在controller中$scope.ceshi绑定。通过ng-model="ceshi" 改变做相应的操作。问题是console.log($scope.ceshi);控制台打印出是undefined。

<input type="checkbox" ng-model="ceshi" >{{ceshi}}  绑定是同一变量,

173610_pEAg_3298482.png

然而 console.log( $scope.ceshi);是undefined

 

171340_DVOQ_3298482.png

 

解决方案:

把状态绑定到对象的属性上  $scope.fromData={"ceshi":false};

<script>
  angular.module('checkboxExample', [])
    .controller('ExampleController', ['$scope', function($scope) {
      $scope.checkboxModel = {
       value1 : true,
       value2 : 'YES'
     };
    }]);
</script>
<form name="myForm" ng-controller="ExampleController">
  <label>Value1:
    <input type="checkbox" ng-model="checkboxModel.value1">
  </label><br/>
  <label>Value2:
    <input type="checkbox" ng-model="checkboxModel.value2"
           ng-true-value="'YES'" ng-false-value="'NO'">
   </label><br/>
  <tt>value1 = {{checkboxModel.value1}}</tt><br/>
  <tt>value2 = {{checkboxModel.value2}}</tt><br/>
 </form>
<input type="checkbox"
       ng-model="string"
       [name="string"]
       [ng-true-value="expression"]
       [ng-false-value="expression"]
       [ng-change="string"]>

 

174525_tl5b_3298482.png

Arguments

ParamTypeDetails
ngModelstring

Assignable AngularJS expression to data-bind to.

name

(optional)

string

Property name of the form under which the control is published.

ngTrueValue

(optional)

expression

The value to which the expression should be set when selected.

ngFalseValue

(optional)

expression

The value to which the expression should be set when not selected.

ngChange

(optional)

string

AngularJS expression to be executed when input changes due to user interaction with the input element.

 

https://my.oschina.net/LinBandit/blog/607306 

https://book.douban.com/subject/20277919/discussion/54472631/

https://segmentfault.com/q/1010000004462678 

http://blog.csdn.net/qq_17371033/article/details/49248791 

转载于:https://my.oschina.net/u/3298482/blog/1530182

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值