I am creating a directive that adds a template with text type input to the view. In this directive, I am trying to add the span class for error notification if the text field input is more than max length setting provided. I have a code something like this:
<div ng-app="app">
<form name="userForm" ng-submit="processForm()" novalidate>
<div use-text-box name="xyz" ng-maxlength="10" required> </div>
<button type="submit" class="btn btn-success">Submit</button>
</form>
</div>
My Directive like this:
var app = angular.module('app', []);
app.directive('useTextBox', function() {
return {
replace:true,
compile: function(element,attrs) {
var name = attrs.name;
var maxLengthError = attrs.hasOwnProperty('ngMaxlength') ? '<span ng-show="userForm.' + attrs.name + '.$error.maxlength" class="help-block">Text is too long. The limit is ' + attrs.ngMaxlength + ' characters.</span>' : '';
var htmlText = '<input type="text" name="' + name + '" ng-maxlength="' + attrs.ngMaxlength + '" required />' +
maxLengthError;
element.replaceWith(htmlText);
}
};
});
But in the above code, the directive is generating the input text field etc.. without a problem. However, it is not showing the error message if the max length is more than 10. What am I doing wrong?
Here is the link to the jsfiddle for the above example: http://jsfiddle.net/fB45J/3/
I don't know if you're just learning and trying to understand directives, but you don't even need a directive to accomplish what you want.
Here's a Fiddle without the directive: http://jsfiddle.net/mikeeconroy/fB45J/7/
Here's the Angular. Its not doing anything much just showing how '$dirty' on a form element works.
EDIT: Here's the fix using your directive approach
http://jsfiddle.net/mikeeconroy/fB45J/8/
You need to inject
$compile
into your directive and then use it to compile your HTML and insert it with the correct scope._element
will be the compiled new element.EDIT: Here's another example using just the
compile
property of a directivehttp://jsfiddle.net/mikeeconroy/dzP9L/1/
I guess it seems that the difference between your example and this one is the introduction of the form's controller.