x
 
<!DOCTYPE html>
<html>
<script src="https://cdn.staticfile.net/angular.js/1.6.9/angular.min.js"></script>
<body ng-app="">
<form name="myForm">
点击此处将输入框设置为必填项:
<input type="checkbox" ng-model="myVar"><br><br>
<input name="myInput" ng-model="myInput" ng-required="myVar">
<h1 ng-if="!myForm.myInput.$valid">输入框不能为空</h1>
</form>
<p>在此例中,如果字段为空且“必填”复选框已被选中,我们将显示一条错误信息。</p>
</body>
</html>