1

I guess that it is a pretty common use case when building an Angular app to use input validation and also display messages to the user telling him/her what´s wrong with the data.

That said, suppose I want to encapsulate some already built-in directive, for example ng-maxlength, to add some default message to show when it is invalid, but I do not want to build it from scratch, while keeping my html as simple and objective as possible.

I could build a directive that has an input text as its template, but then I would have to build one for each case or treat every single permutation in one directive right? That sounds a bit messy to me.

Let me show what is my goal:

<input type="text" ng-model="model.description" ng-maxlength-with-message="[50, 'The description should not be more than 50 letters']"/>

That way I can pass the message and the max-length together so my directive can observe that error and display the message if needed. The way it shows the message is irrelevant (probably by appending some html and bind it with ng-show), the key here is to do that using the already well tested and established ´ng-maxlength´.

Did that make any sense?

Is it even possible to do it?

Oleg
  • 8,818
  • 2
  • 40
  • 55
Fedaykin
  • 4,262
  • 3
  • 20
  • 32
  • 1
    You might be interested in http://stackoverflow.com/questions/17005122/extending-angular-directive – Oleg Aug 25 '14 at 19:41
  • great! I´ll try that out! I don´t know why I didn´t search for "extension" before – Fedaykin Aug 25 '14 at 19:45

0 Answers0