使用ng-repeat时如何切换复选框?

时间:2018-02-20 03:24:14

标签: html angularjs

当在由ng-repeat生成的一组开关中使用开关时,我遇到了问题。我的目标是在单击每个复选框时触发警报。

例如,当我点击开关启用它时应该警告" 1",然后关闭应警告" 0",但当我点击开关启用它警报&# 34; 1"当我点击另一个开关启用它时,警告" 0"。

Here is a plunkr with a sample single switch

<!DOCTYPE html>
<html>

<head>
  <link rel="stylesheet" href="style.css" />
  <script src="script.js"></script>
</head>

<body ng-app="switchdemo">
  <h1>On Off switch using Angular JS</h1>
  <div ng-controller="DemoController" ng-init="init()">
    <div class="well">
 <label class="switch" >
                <input type="checkbox" ng-model="Token" ng-change="changeStatus();">
                <span class="slider round"></span>
              </label>
              </div>
        <div class="well">
       <label class="switch" >
                <input type="checkbox" ng-model="Token" ng-change="changeStatus();">
                <span class="slider round"></span>
              </label></div>
    <pre>{{ status }}</pre>
  </div>
</body>

</html>

AngularJS:

angular.module('switchdemo', []).controller('DemoController', function($scope){

  $scope.init = function(){
    $scope.status = 1;
  }

  $scope.changeStatus = function(){
    $scope.status = !$scope.status;
    if($scope.status==1) {
      $scope.status=1;
      alert($scope.status);
    }
      else
      {
        $scope.status=0;
         alert($scope.status);
    }
  }

})

1 个答案:

答案 0 :(得分:0)

在html中

<div ng-controller="MyCtrl">
    <input type="checkbox" ng-model="Token" ng-change="changeStatus(Token);">
</div>

在javascript中,您将获得true / false到警报参考链接http://jsfiddle.net/ADukg/18311/

var myApp = angular.module('myApp',[]);

function MyCtrl($scope) {
    $scope.changeStatus = function(data){
        alert(data)
    };
}