值不会在复选框中自动分配

Values are not automatically assigning in checkbox

我有一个复选框来显示停用帐户,当在请求中选中复选框时,我需要将激活状态发送为 false 以显示停用帐户,当未选中时,它应该将值传递为 true。我的复选框在初始化后只是传递相同的值 $scope.checkedStatus = true.Values 没有传递为 false.

HTML :

<div>
<input type="checkbox" ng-model="checkedStatus" ng-click = "viewAccount">
  Show Deactivated Account
</div> 

JS:

 $scope.checkedStatus = true;
 $scope.viewAccount = function(){

              var json = {
  "json": {
    "request": {
      "servicetype": "6",
      "functiontype": "6014",
      "session_id": $rootScope.currentSession,
           "data": {
        "shortname": $scope.model.selectShortName,
        "groupzname": $scope.model.selectGname,
        "city": $scope.model.selectCity,
        "state": $scope.model.selectState,
        "country": $scope.model.selectCountry,
        "groupzcode": $scope.model.selectGcode,
         "activationstatus": !($scope.checkedStatus),
        "details": false,
        "sortbasedon": $scope.groupzname,
        "orderby": "desc",
        "offset":$scope.pagination
           }

    }
  }
};

    UserService.viewListAccount(json).then(function(response) {
    if (response.json.response.statuscode == 0)
                        {
                   $scope.tableData = response.json.response.data; 
                        }
                }); 
        };

复选框不会自动更改值。

你可以使用

ng-change

在输入中。 在这里你可以在 html 中完成。

<input type="checkbox" ng-model="checkedStatus" ng-click = "viewAccount" ng-change="change()">
  Show Deactivated Account
</div> 

这是您在控制器中访问它的方式。

$scope.change = function() {
  $scope.viewAccount();
}

在您的 json 中,您只是否定了 $scope.checkedStatus 但没有改变它的值 。所以你总是得到相同的结果。
您可以 更改范围值,而不是在 json 中使用 !($scope.checkedStatus)

 $scope.checkedStatus = true;
 $scope.viewAccount = function(){
   $scope.checkedStatus = !$scope.checkedStatus; //check this line
    var json = {
    "json": {
      "request": {
        "servicetype": "6",
        "functiontype": "6014",
          .
          .
          "activationstatus": $scope.checkedStatus,
          .
          .
          "orderby": "desc",
          "offset":$scope.pagination
             }

      }
    }
};

在您的 $scope 中创建一个包装器对象 model 并在其中创建 checkedStatus 属性。

$scope.model = {
    checkedStatus: true
};

并对引用的地方进行相关更改。

像 html 一样进行这些更改

ng-click="viewAccount()" ng-model="model.checkedStatus"

另请注意,您永远不应该直接在作用域上附加属性,并且始终尝试创建包装器对象或使用控制器作为语法来消除此类问题。

查看这篇文章:AngularJS: If you don't have a dot, you're doing it wrong - See more at: http://zcourts.com/2013/05/31/angularjs-if-you-dont-have-a-dot-youre-doing-it-wrong/#sthash.NDNvWL5E.dpuf

对于复选框 angular 有一个名为 ng-change 的特殊指令,请改用它。

<div>
   <input type="checkbox" ng-model="checkedStatus" ng-change="viewAccount()">
   Show Deactivated Account
</div>