可以Angular ng-if检查值是否包含某些内容?

时间:2014-09-17 10:23:19

标签: angularjs angularjs-directive

我的模板中有以下内容:

<td ng-if="invoice.paymentMethod == 'automatic'">{{ invoice.paid | date:'yyyy-MM-dd hh:ss' }}</td>
<td ng-if="invoice.paymentMethod != 'automatic'">{{ invoice.paid | date:'yyyy-MM-dd' }}</td>

在自动付款时,它以不同的格式(按时间)显示付款日期。

但是:将来,这个“invoice.paymentMethod”可以变成“自动 - 理想”或“自动 - 贝宝”。

所以,我不能制作一个ng-if,以便检查invoice.paymentMethod是否包含“自动”而不是等于它?应该很容易但我无法在任何地方找到它。

1 个答案:

答案 0 :(得分:0)

在这种情况下,ngSwitch将完美运行,请参阅此处的文档

  

https://docs.angularjs.org/api/ng/directive/ngSwitch

或看到该演示:http://jsbin.com/qejoza/1/edit或此处http://jsbin.com/qejoza/2/edit

HTML:

 <table ng-repeat="invoice in invoices" ng-switch on="invoice.paymentMethod">
    <tr>
      <td ng-switch-when="automatic">automatic: {{ invoice.paid | date:'yyyy-MM-dd hh:ss' }}</td>
      <td ng-switch-when="automatic-ebay">ebay: {{ invoice.paid | date:'yyyy-MM-dd hh:ss' }}</td>
      <td ng-switch-when="automatic-paypal">paypal:{{ invoice.paid | date:'yyyy-MM-dd hh:ss' }}</td>
    </tr>  
  <table>

JS:

var app = angular.module('app', []);

app.controller('firstCtrl', function($scope){


 $scope.invoices=[{

   paymentMethod: 'automatic-paypal',
   paid: "16/01/2014"
 },
                 {

   paymentMethod: 'automatic-ebay',
   paid: "16/01/2014"
 },
                 {

   paymentMethod: 'automatic',
   paid: "16/01/2014"
 }];


});