ngMessages is required here in your module, but not in your controller.
ngMessages is a directive, not a service, look at this documentation
so you define it as a requirement for your angularJs module
angular.module('myModule', ['ngMessages']);
then you can use in your html like this:
<form name="myForm"> <input type="text" ng-model="field" name="myField" required minlength="5" /> <div ng-messages="myForm.myField.$error"> <div ng-message="required">You did not enter a field</div> <div ng-message="minlength">The value entered is too short</div> </div> </form>
EDIT: this is a working sample
1- Index.html should be:
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> </head> <body ng-app="registerApp" ng-controller="RegistrationCtrl"> <form name="myForm"> <input type="text" ng-model="field" name="myField" required minlength="5" /> <div ng-messages="myForm.myField.$error"> <div ng-message="required">You did not enter a field</div> <div ng-message="minlength">The value entered is too short</div> </div> </form> <script src="Scripts/angular.js"></script> <script src="Scripts/angular-messages.js"></script> <script src="app.js"></script> </body> </html>
2- app.js
angular .module('registerApp', ['ngMessages']) .controller('RegistrationCtrl', ['$scope', function ($scope) { var vm = this; }]);
Edit 2: mainly for ngMessages, because it is a directive, you only need to add the module that was defined in the dependencies of the application module, and you do not need to enter anything into the controller. Perhaps you can get error messages from the controller.
Hope this helps.
source share