2014-12-04 54 views
1

我的代码如下:Angularjs:检查是否输入是空的,NG-形式

<ng-form name="invitationForm"> 
<div ng-repeat="email in emails" ng-form='lineForm'> 
    <div class="has-feedback" ng-class="{'has-error': lineForm.$invalid && lineForm.input, 'has-success': lineForm.input && !lineForm.$invalid}"> 
     <label class="control-label sr-only" for="inputSuccess">Input with success</label> 
     <input ng-change="inputCheck($index)" id="inputSuccess" placeholder="Enter your friend's email" name="input" ng-model="email.value" class="form-control input-mini" aria-describedby="inputSuccessStatus" type="email" /> 
    </div> 
</div> 
</ng-form> 

我想要做的就是有类格“有反馈”上课“了 - 错误”当lineForm无效并且输入不为空时。 我把条件线形$无效& & lineForm.input但lineForm.input似乎没有工作。 我也试过lineForm.input.length无济于事。

感谢

+1

我认为你是在复杂的事情,如果你正在使用的角度1.3,通过yearofmoo的文章阅读表格和验证 - http://www.yearofmoo.com/2014/09/taming-forms-in-angularjs-1-3.html – 2014-12-04 21:48:25

回答

1

http://plnkr.co/edit/se8iBxlUK6J0oS8lUWTn?p=preview

我不认为你需要创建多种形式。看看这个例子(几个元素与所需的输入:

<form role="form" name="theform"> 

    <div ng-repeat="number in [1,2,3]"> 
    <input type="text" 
      name="{{$index}}" 
      ng-model="number" 
      required="" /> 
    </div> 

    {{ theform.$valid ? 'yes, valid form' : 'no, invalid' }} 

    <br/><br/> 

    <div ng-repeat="element in theform"> 
    {{ element.$valid }} 
    </div> 

    <br/> 
    Explore this: 
    <br/><br/> 
    {{ theform }} 

</form> 

有关您可以用的FormController做进一步的例子: https://docs.angularjs.org/api/ng/type/form.FormController