Show Required error

Description

The following code shows how to use $error to show required validation error.

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   ww  w . j  ava 2s  . com-->
    $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