Angular ng-show如何使可见

时间:2016-01-03 15:24:18

标签: javascript angularjs angularjs-ng-show

使用简单的联系表单,如果$ 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);
  }

}
}])

2 个答案:

答案 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