1

I am trying to check if a checkbox is defined and unchecked it. On load my checkbox is disabled and I am getting the error that is undefined. I try to put a condition to avoid the error but didn't work

 if ( typeof $scope.user.test !== 'undefined')
    {
        $scope.user.test = false;
    }

 <md-checkbox ng-model="user.test" layout="row" ng-disabled="userForm.$invalid">
      Test
 </md-checkbox>

1 Answers1

0

https://plnkr.co/edit/h2t3K0Fwxpo5gAhAeqqt?p=preview here is working plunker. You may forgot to include all the below dependencies. Now you can do your work.

<html lang="en" >
   <head>
      <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/angular_material/1.0.0/angular-material.min.css">
      <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
      <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-animate.min.js"></script>
      <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-aria.min.js"></script>
      <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-messages.min.js"></script>
      <script src="https://ajax.googleapis.com/ajax/libs/angular_material/1.0.0/angular-material.min.js"></script>
      <script language="javascript">
         angular
            .module('firstApplication', ['ngMaterial'])
            .controller('checkBoxController', checkBoxController);

         function checkBoxController ($scope) {    
             $scope.call = function(){
                 alert($scope.user.test)  
             }
         }  
      </script>      
   </head>
   <body ng-app="firstApplication" ng-controller="checkBoxController">  
      <md-checkbox ng-model="user.test" ng-change="call()" aria-label="No Ink Effects">
         Check
      </md-checkbox>
   </body>
</html>
Mr_Perfect
  • 8,254
  • 11
  • 35
  • 62
  • 1
    No, he is comparing the type of `$scope.user.test` to `"undefined"`, which is correct http://stackoverflow.com/questions/2703102/typeof-undefined-vs-null – James Monger Dec 29 '16 at 12:59
  • Thank you . I changed it. The same issue. –  Dec 29 '16 at 13:00