The following code shows how to use $error
to show required validation error.
<!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 .j a v a2 s.co 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: