angularjs怎么实现checkbox选中触发事件?

html代码:

<input type="checkbox" ng-click="sub(1)">选择



<div ng-show="showType==1" class="btn">
      <button type="submit">提交申请</button>
        </div>


        <div ng-show="showType==2" class="btn">
      <button type="submit">提交申请</button>
        </div>

js代码:

var app = angular.module('test', []);
    app.controller('chkCtrl', function($scope){
         $scope.showType=1;
        $scope.sub = function(type){
            if(type==1){
         $scope.showType =2;
            }
            else if( $scope.showType ==2){
                $scope.showType =1;
            }
            };

这代码点击勾选的时候有效,但是取消勾选就没效了。请问要怎么样修改?

阅读 13.8k
2 个回答

$scope.sub()的参数去掉,然后在方法体里面加一句让$scope.showType取反

HTML

<input type="checkbox" ng-click="sub()">选择



<div ng-show="showType==1" class="btn">
  <button type="submit">提交申请</button>
</div>





<div ng-show="showType!=1" class="btn">
  <button type="submit">提交申请</button>
</div>


JS

var app = angular.module('test', []);
app.controller('chkCtrl', function($scope){
    $scope.showType=1;
    $scope.sub = function(){
        $scope.showType=!$scope.showType;
    }
};

是这意思不?

你的sub始终传参都是1,当然不会有变化咯

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题