ng-required,如何在输入之间使用它?

时间:2015-10-07 13:17:47

标签: javascript html angularjs input typescript

我为X元素创建了一个重复两个输入的表单。只有当我们把东西放在另一个元素中时才需要每个元素:

My.HTML

<form ng-submit="validate();">
 <div ng-reapet="elem in Myelements">
  <label>{{elem.name}}</label>
  <div>
    New Value : <input name="value" ng-required="reason.$valid" ng-model="elem.value" />
  </div>
  <div>
    Why : <textarea name="reason" ng-required="value.$valid" ng-model="elem.reason"/>
  </div>
 </div>  
 <input type="submit" value="Submit" />
</form>

这不按预期工作。我该如何正常工作?

编辑 适合我的解决方案(接受的答案对我有帮助)

<form ng-submit="validate();">
    <div ng-reapet="elem in Myelements">
        <label>{{elem.name}}</label>
        <div>
            New Value : <input name="value" ng-required="elem.reason" ng-model="elem.value" />
        </div>
        <div>
            Why : <textarea name="reason" ng-required="elem.value" ng-model="elem.reason"/>
        </div>
    </div>  
    <input type="submit" value="Submit" />
</form> 

1 个答案:

答案 0 :(得分:1)

只需关闭其他输入值

<form ng-submit="validate();">
    <div ng-reapet="elem in Myelements">
        <label>{{elem.name}}</label>
        <div>
            New Value : <input name="value" ng-required="elem.reason !== ''" ng-model="elem.value" />
        </div>
        <div>
            Why : <textarea name="reason" ng-required="elem.value !== ''" ng-model="elem.reason"/>
        </div>
    </div>  
    <input type="submit" value="Submit" />
</form>