为什么即使输入字段有输入,Angular ng-message' when =“required”'也会触发?

时间:2016-04-19 08:20:54

标签: javascript angularjs

我有plunker here

当我在输入字段中输入输入并单击页面上的其他位置时,会显示消息“此字段是必需的”。得到显示。尽管输入字段具有值?

,为什么显示消息?

以下是代码:

<form name="reportform" novalidate >
  <input name="startdate" placeholder="Enter a start date" ng-model="startdatevalue" required>
  <ng-messages ng-if='reportform.startdate.$touched' for="reportform.startdate.$error">
    <ng-message when="required">
      This field is required.
    </ng-message>
  </ng-messages>
  <button ng-disabled="reportform.$invalid" type="submit">
    Submit Query
  </button>
</form>

2 个答案:

答案 0 :(得分:1)

您缺少导入ngMessages模块:

示例:https://plnkr.co/edit/VWhrTIzRjkQBGfjaj5Zb?p=preview

 var app = angular.module('plunker', ['ngMessages']);

app.controller('MainCtrl', function($scope) {
  $scope.name = 'World';
});

答案 1 :(得分:0)

以下是updated Demo

只需更改即可在角度模块中添加依赖项ngMessages

 var app = angular.module('plunker', ['ngMessages']);

 app.controller('MainCtrl', function($scope) {
 $scope.name = 'World';
 });