1

I have a checkbox to show deactivate account , when check box is selected in request I need to send activationstatus as false for showing deactivate account and when unchecked it should pass value as true. My checkbox is just passing same value after initialising $scope.checkedStatus = true.Values are not passing as 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; 
                        }
                }); 
        };

Automatically checkbox is not changing the value.

Nicoleta Wilskon
  • 687
  • 1
  • 10
  • 32

4 Answers4

0

you can use

ng-change

in input . here you can do it in html.

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

this how you will access it in your controller.

$scope.change = function() {
  $scope.viewAccount();
}
Ameer Hamza
  • 586
  • 3
  • 16
0

In your json you are just negating the $scope.checkedStatus, but not changing its value. So always you are getting the same result.
Instead of using !($scope.checkedStatus) in your json, you can just change the scope value.

 $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
             }

      }
    }
};
Sravan
  • 18,467
  • 3
  • 30
  • 54
0

create a wrapper object model in your $scope and create checkedStatus property in it.

$scope.model = {
    checkedStatus: true
};

and make relevant changes where ever its referenced.

Like in html make these changes

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

Also as a side note, you should never directly attach properties on scope and always try to create a wrapper object or use a controller as syntax to get rid of such issues.

Check out this article: 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

Nishant
  • 1,034
  • 9
  • 19
0

For checkboxes angular has special directive called ng-change use that instead.

<div>
   <input type="checkbox" ng-model="checkedStatus" ng-change="viewAccount()">
   Show Deactivated Account
</div> 
Hamza Baig
  • 666
  • 1
  • 7
  • 13