javascript - Angular validators and ng-maxlength use -


i've got following div, want add bootstrap's class "has-error" if input length on 50 characters. html:

<div class="form-group" ng-class="{has-error:[formdata.titulo.$error]}">      <label for="inputtitulo">título</label>     <input type="titulo" class="form-control" id="inputtitulo"             maxlength="50" ng-maxlength="50" ng-model="formdata.titulo">  </div> 

how can make work? guess when reach 50 characters, ng-maxlength throws error, $error object, have no clue on object is, how access it, , if have more work in controller or directive.

any here? can't find "easy" info regarding issue , angular validators.

edit 1:

i've seen responses, learned new you, still somehow not working. way:

  <div class="form-group" ng-class="{'has-error': formdata.titulo.$error.maxlength}">     <label for="inputtitulo">título</label>     <input type="titulo" class="form-control"  id="inputtitulo" maxlength="50" ng-maxlength="50" ng-model="formdata.titulo">   </div> 

also tested checking length directly, 1 of suggested. none of these solutions seem work: never adds has-error class. why?

to have errors published on scope, form directive required.

<div ng-form="form1" ng-class="{'has-error': form1.text1.$error.maxlength}">   <input name="text1" ng-model="formdata.foo" ng-maxlength="50"> </div> 

(notice above uses name attribute of input publish form data - really, ngmodelcontroller - on scope)

so, above works, , it's preferable if form validation. but, if need check length of input, don't have use form validation - check model directly:

<div ng-class="{'has-error': formdata.foo.length > 50}>   <input ng-model="formdata.foo"> </div> 

Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -