即使没有匹配的记录,如何显示输入框

时间:2016-09-07 20:14:57

标签: javascript angularjs

我的数据库中有5本书。其中只有3个有价格。如果数据库中没有价格,我怎么还能显示输入框?如果books和bookPrices表中存在匹配的Id,则下面的代码仅创建一个输入框。我是初学者,如果可能请在回答时使用我的示例代码。 :-)

我有5本书。只有书籍1,3和& 5有价格。第2项和第2项将没有输入框。 4.我希望所有人都有一个输入框,以便用户输入价格并保存。

<div ng-repeat="book in books">
  <div>
     <label>{{ book.Name }}</label>
     <div ng-repeat="price in bookPrices | filter : { bookId : book.bookId } ">
        <div>
           <input type="text" class="form-control" name="val" ng-model="price.Value" maxlength="100">
        </div>
     </div>
  </div>
</div>

1 个答案:

答案 0 :(得分:1)

如果没有值,您可以使用角度条件显示输入框。用下面的代码替换第4行和第5行。

<span>{{price.Value}}</span>
<div ng-if="price.Value == "" || price.Value == undefined">
  <input type="text" class="form-control" name="val" ng-model="price.Value" maxlength="100">
</div>

或者,如果您使用的角度版本早于1.1.5,则可以使用ng-show

<span>{{price.Value}}</span>
<div ng-show="price.Value == "" || price.Value == undefined">
  <input type="text" class="form-control" name="val" ng-model="price.Value" maxlength="100">
</div>