angularjs ngMessages inside directive

非 Y 不嫁゛ 提交于 2019-12-11 10:33:28

问题


I have created a directive with Username and ngMessage for validation. But ngMessage does not get displayed when validation occurs.

created a plunker. can anybody help me

<!DOCTYPE html>
<html ng-app="app">

<head>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular-messages.min.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular-route.min.js"></script>

  <style>
    input.ng-invalid {
      border: 1px solid red;
    }
  </style>
  <script>
    var module = angular.module('app', ['ngRoute', 'ngMessages']);
  </script>
  <script src="config.js"></script>
  <script src="regdirective.js"></script>
</head>

<body>
  <ng-view></ng-view>
</body>

</html>

http://plnkr.co/edit/JrKzLuh739gcqGFP71jn?p=preview


回答1:


You did wrong in your embedded html. I changed your source code to:

template:'<div>User Name<input type="text" ng-model="reg.Username" name="Username" class="form-control" required ng-minlength="5" ng-maxlength="20" /></b></div>' +
    '<br><div ng-messages="regForm.Username.$error" style="color:maroon" role="alert">' +
    '<div ng-message="required">Enter User Name</div>' +
    '<div ng-message="minlength">User name is too short</div>' +
    '<div ng-message="maxlength">User Name is too long</div></div>',

And it worked here: http://plnkr.co/edit/onlfdhraHipjezcgIg7f?p=preview



来源:https://stackoverflow.com/questions/33429542/angularjs-ngmessages-inside-directive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!