检查是否有任何角度输入有输入

时间:2016-11-28 21:31:35

标签: javascript html angularjs

所以我试图查看一系列输入是否有数据,如果有,则输出到其他地方。我已经将ng-model应用于具有这些输入的div,但是没有得到我想要的结果。

我的输入代码:

<div class="benefits" ng-model="benefits">          
    <input type="text" class="form-control" ng-model="benefit1" placeholder="benefit 1" style="font-size:18px; margin-bottom: 25px;">
    <input type="text" class="form-control" ng-model="benefit2" placeholder="benefit 2" style="font-size:18px; margin-bottom: 25px;">
    <input type="text" class="form-control" ng-model="benefit3" placeholder="benefit 3" style="font-size:18px; margin-bottom: 25px;">       
    <input type="text" class="form-control" ng-model="benefit4" placeholder="benefit 4" style="font-size:18px; margin-bottom: 25px;">
    <input type="text" class="form-control" ng-model="benefit5" placeholder="benefit 5" style="font-size:18px; margin-bottom: 25px;">   
</div>  

我的输出(无法正常工作:

<ul class="outputText" ng-show="benefits != null">Perceived benefits:
    <li>{{ (benefit1 != null) ? benefit1 : '[describe benefit 1]' }}</li>
    <li>{{ (benefit2 != null) ? benefit2 : '[describe benefit 2]' }}</li>
    <li>{{ (benefit3 != null) ? benefit3 : '[describe benefit 3]' }}</li>
    <li>{{ (benefit4 != null) ? benefit4 : '[describe benefit 4]' }}</li>
    <li>{{ (benefit5 != null) ? benefit5 : '[describe benefit 5]' }}</li>
</ul>

如果div上的ng-model不是要走的路,我怎么能设置一个benefit1到benefit5的范围,输出才能显示简单的东西?

我确信这是一件简单的事情,我只是没有看到?

非常感谢。

1 个答案:

答案 0 :(得分:3)

尝试使用<form>并使用指定表单的$dirty属性。

&#13;
&#13;
angular.module("MyApp", [])
  .controller("MyController", function() {});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="MyApp">
  <div ng-controller="MyController">

    <div class="benefits">
      <form name="benefits">
        <input type="text" class="form-control" ng-model="benefit1" placeholder="benefit 1" style="font-size:18px; margin-bottom: 25px;">
        <input type="text" class="form-control" ng-model="benefit2" placeholder="benefit 2" style="font-size:18px; margin-bottom: 25px;">
        <input type="text" class="form-control" ng-model="benefit3" placeholder="benefit 3" style="font-size:18px; margin-bottom: 25px;">
        <input type="text" class="form-control" ng-model="benefit4" placeholder="benefit 4" style="font-size:18px; margin-bottom: 25px;">
        <input type="text" class="form-control" ng-model="benefit5" placeholder="benefit 5" style="font-size:18px; margin-bottom: 25px;">
      </form>
    </div>

    <ul class="outputText" ng-show="benefits.$dirty">Perceived benefits:
      <li>{{ (benefit1) ? benefit1 : '[describe benefit 1]' }}</li>
      <li>{{ (benefit2) ? benefit2 : '[describe benefit 2]' }}</li>
      <li>{{ (benefit3) ? benefit3 : '[describe benefit 3]' }}</li>
      <li>{{ (benefit4) ? benefit4 : '[describe benefit 4]' }}</li>
      <li>{{ (benefit5) ? benefit5 : '[describe benefit 5]' }}</li>
    </ul>

  </div>
</div>
&#13;
&#13;
&#13;