Why does Angular provide ng-maxlength when there is a standard maxlength?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-01 14:01:46

问题


HTML provides a standard maxlength attribute since version 4.01. Angular provides a ng-maxlength directive.

The difference is that the standard approach doesn't allow entering more than max, while Angular's approach just generates a validation error.

Why did they decide to include such a directive? It's not because of dynamic data binding of maxlength since the behaviour is inconsistent. It's not because of compatibility since it's available even in HTML 4.01. My only guess is that it's there to provide an alternative validation paradigm, but it sounds like over-engineering.


回答1:


Not only it provide validation for max length, it also adds a class ng-invalid-maxlength. so when you put anything beyond the limit ng-invalid-maxlength class will placed there and in that class you can write your own css.

So after key press if validation fails, the css will be invoked simultaneously and reflects on your UI.




回答2:


The previous answer may of been correct in earlier versions of angular but as of today and Angular v1.5.5, it looks like maxlength does exactly the same thing as ng-maxlength but also enforces the HTML inputs maxlength property on inputs of type text.

Example:

<input type="text" maxlength=5>

maxlength will enforce a 5 character max on the input and also add the correct angular validation class to the input.

Whereas ngMaxlength will only enforce validation. See: https://docs.angularjs.org/api/ng/directive/ngMaxlength

Advise: Just use maxlength.



来源:https://stackoverflow.com/questions/26907669/why-does-angular-provide-ng-maxlength-when-there-is-a-standard-maxlength

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