使用简单的联系表单,如果$ scope未定义,我需要显示错误消息。
验证部分有效但我不记得如何在html端的ng-show下启用消息视图。
HTML:
<p class="response error" ng-show="error">{{errorMessage}}</p>
JS:
var $emptyContent = 'Please enter your prices.';
app.controller('priceForm', ['$scope', '$http', function($scope, $http){
$scope.formData;
// Process the form data
$scope.submitForm = function () {
console.log($scope.formData);
if($scope.formData == null) {
// Show empty content message
console.log($emptyContent);
}
}
}])
答案 0 :(得分:1)
只需在你的控制器中error
标记<{1}} 或,就像@ryanyuyu在评论中建议的true
一样。
<强>代码强>
ng-show="errorMessage"
答案 1 :(得分:0)
您不希望创建使用预定义对象来解决此问题的自定义错误范围
<form name="myForm">
<label>
Enter your name:
<input type="text"
name="myName"
ng-model="name"
ng-minlength="5"
ng-maxlength="20"
required />
</label>
<pre>myForm.myName.$error = {{ myForm.myName.$error | json }}</pre>
<div ng-messages="myForm.myName.$error" style="color:maroon" role="alert">
<div ng-message="required">You did not enter a field</div>
<div ng-message="minlength">Your field is too short</div>
<div ng-message="maxlength">Your field is too long</div>
</div>
</form>
https://docs.angularjs.org/api/ngMessages/directive/ngMessages