代码之家  ›  专栏  ›  技术社区  ›  Mr world wide

使用ng更改检查的ng不起作用[重复]

  •  -1
  • Mr world wide  · 技术社区  · 8 年前

    这是我的angularjs代码

    <label class="switch">
        <input
        type="checkbox"  
        ng-model="question.questionId" 
        ng-change="questionStatusChange(question.questionId,!question.active)" 
        ng-checked="question.active" >
        <span class="slider round"></span>
    </label>
    

    ng-change 正在从触发罚款 未选中 到 已检查 状态 但是 ng-change="questionStatusChange(question.questionId,!question.active)" 不是从工作 已检查 到 未选中 状态

    我正在尝试启用或禁用表记录。

    $scope.questionStatusChange = function(questionId,status) {
    console.log(status);
    }
    

    我正在使用此切换: https://www.w3schools.com/howto/howto_css_switch.asp

    1 回复  |  直到 8 年前
        1
  •  1
  •   Jimenemex    8 年前

    你的问题是 ng-model 和 ng-checked 不能一起使用。

    你应该可以 ng型号 ,并将其绑在 boolean 属性。去除 ng已检查 从html和 ng型号 。

    编辑: 下面显示了如何使用 ng-repeat 创建列表的步骤 <input> 其中每个部分都有自己的对象 $scope.questions[] .完成后,您可以使用 ng型号 并将其绑定到 questions[i].active 直接地

    var app = angular.module("MyApp", []);
    
    var MyController = function($scope) {
    
      $scope.questions = [{
        questionId: 1,
        active: false,
        questionText: "First question"
      }, {
        questionId: 2,
        active: true,
        questionText: "Second question"
      }];
      $scope.questionStatusChange = function(currentQuestion) {
        //currentQuestion.active = !currentQuestion.active;
        console.log("Current Question Text: " + currentQuestion.questionText + "\nIs it clicked? " +  currentQuestion.active);
      }
    }
    
    app.controller(MyController, "[$scope, MyController]");
    <html>
    
    <head>
      <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
    </head>
    
    <body ng-app="MyApp">
      <div ng-controller="MyController">
        <div ng-repeat="question in questions">
          <label class="switch">
            <input
            type="checkbox"  
            ng-model="question.active"
            ng-change="questionStatusChange(question)" />
            <span class="slider round"></span>
          </label>
        </div>
      </div>
    </body>
    
    </html>