Negate value with ng-click

Description

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) {<!--   ww 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>

Click to view the demo





















Home »
  AngularJS »
    AngularJS Example »




Controller
Directives
Expression
Filter
Form
Inject
Scope
Server
Style
Template
Utilities