【问题标题】:Angularjs : check if input is empty with ng-formAngularjs:使用 ng-form 检查输入是否为空
【发布时间】:2014-12-04 21:40:41
【问题描述】:

我的代码如下:

<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无效且输入不为空时,我想要做的是让具有类“has-feedback”的div具有类“has-error”。 我设置了条件 lineform.$invalid && lineForm.input 但 lineForm.input 似乎不起作用。 我也试过 lineForm.input.length 无济于事。

谢谢

【问题讨论】:

标签: angularjs forms input is-empty


【解决方案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

【讨论】:

    猜你喜欢
    • 2010-12-23
    • 2018-05-26
    • 2020-09-17
    • 1970-01-01
    • 2015-01-11
    • 2013-03-05
    • 2018-04-18
    • 1970-01-01
    相关资源
    最近更新 更多