Angularjs显示属性等于的项目的长度

时间:2016-08-10 08:38:51

标签: angularjs angularjs-scope angularjs-ng-repeat

我有这个HTML标记:

<table class="table tenant-table text-center">
  <tr>
    <th class="text-center">Property Number</th>
    <th class="text-center">Location</th>
    <th class="text-center">Cost / Value</th>
    <th class="text-center">Commercial Tenants</th>
    <th class="text-center">Multy-family Tenants</th>
    <th></th>
  </tr>

  <tr ng-repeat="property in purchasePropertyData.properties">
    <td>{{property.PropertyType}}</td>
    <td>{{property.City}}</td>
    <td>&euro;{{property.CurrentValue}}</td>
    <td>{{property.Tenants.length}}</td> // where tenanttype = 1
    <td>{{property.Tenants.length}}</td> // where tenanttype = 2
  </tr>
</table>

我想知道我是否可以直接访问租户类型等于1或2的房产中的租户长度。

我知道控制器中有一些方法我可以计算出来,但我想知道是否有可能直接这样做。

4 个答案:

答案 0 :(得分:0)

你是说这样的意思吗?您只想在{{property.Tenants.length}}等于1或2时显示tenanttype的位置?

<tr ng-repeat="property in purchasePropertyData.properties">
    <td>{{property.PropertyType}}</td>
    <td>{{property.City}}</td>
    <td>&euro;{{property.CurrentValue}}</td>
    <td>{{property.Tenants.length}}</td>
    <td ng-if="property.tenanttype === 1 || property.tenanttype === 2">{{property.Tenants.length}}</td>
</tr>

<强>更新

<tr ng-repeat="property in purchasePropertyData.properties">
    <td>{{property.PropertyType}}</td>
    <td>{{property.City}}</td>
    <td>&euro;{{property.CurrentValue}}</td>
    <td>{{property.Tenants.length}}</td> // where tenanttype = 1
    <td ng-if="property.tenanttype === 1">
        {{(property.Tenants | filter:{tenanttype:1}).length}}
    </td>
    <td ng-if="property.tenanttype === 2">
        {{(property.Tenants | filter:{tenanttype:2}).length}}
    </td>
</tr>

更新:当值不存在时显示0

<tr ng-repeat="property in purchasePropertyData.properties">
    <td>{{property.PropertyType}}</td>
    <td>{{property.City}}</td>
    <td>&euro;{{property.CurrentValue}}</td>
    <td>{{property.Tenants.length}}</td> // where tenanttype = 1
    <td ng-if="property.tenanttype === 1">
        {{(property.Tenants | filter:{tenanttype:1}).length}}
    </td>
    <td ng-if="property.tenanttype === 2">
        {{(property.Tenants | filter:{tenanttype:2}).length}}
    </td>
    <td ng-if="!property.Tenants">0</td>
</tr>

答案 1 :(得分:0)

或只是在一个表达式中:

<td>{{(property.Tenants | filter:{tenanttype:1}).length }}</td>
<td>{{(property.Tenants | filter:{tenanttype:2}).length }}</td>

答案 2 :(得分:0)

<tr ng-repeat="property in purchasePropertyData.properties">
    <td>{{property.PropertyType}}</td>
    <td>{{property.City}}</td>
    <td>&euro;{{property.CurrentValue}}</td>
    <td><span ng-if="property.Tenants.indexOf(1) > -1" ng-bind="property.Tenants.length"></span></td>
    <td><span ng-if="property.Tenants.indexOf(2) > -1" ng-bind="property.Tenants.length"></span></td>
</tr>

答案 3 :(得分:-1)

你可以试试这个:

<tr ng-repeat="property in purchasePropertyData.properties">
    <td ng-if='property.Tenants' ng-repeat="item in property.Tenants | filter : {tenanttype:1}" ng-if="$last">{{$index + 1}}</td>// where tenanttype = 1
    <td ng-if='property.Tenants' ng-repeat="item in property.Tenants | filter : {tenanttype:2}" ng-if="$last">{{$index + 1}}</td>// where tenanttype = 2
    <td colspan="2" ng-if='!property.Tenants'>0</td>
</tr>