AngularJS Tutorial - Negate value with ng-click








The following code shows how to Negate value with ng-click.

Example


<!DOCTYPE html>
<html  ng-app>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.js"></script>
<script type='text/javascript'>
function Ctrl($scope) {<!--from w w w. ja  v a 2 s  .c  o m-->
    $scope.myData = {
        value: '',
        isOptional: true
    };
}
</script>
</head>
<body>
  <div ng-app>
  <div ng-controller="Ctrl">
    <form name="myForm">
      <input ng-model="myData.value" name="mydata_value" type="text" ng-required="myData.isOptional">
      <span ng-show="myForm.mydata_value.$error.required">
        required</span>
      <button ng-click="myData.isOptional=!myData.isOptional">toggle isOptional</button>
    </form>
  </div>
</div>
</body>
</html>

The code above is rendered as follows: