如果angularjs中的数组为空,则显示一条消息

时间:2017-05-04 16:28:24

标签: angularjs ionic-framework accordion

我想写一条消息,如果列表中没有任何内容,则列表为空。如果我的表是空的,我希望它显示“无组”或“无类别”之类的消息。

我的代码:

<ion-list>
    <ion-item class="item-stable" ng-click="toggleGroup(group)" ng-class="{active: isGroupShown(group)}">
      <i class="icon" ng-class="isGroupShown(group) ? 'ion-minus' : 'ion-plus'"></i>
      &nbsp; Group 1 :
    </ion-item>
    <ion-item class="item-accordion" ng-repeat="item in group track by $index" ng-show="isGroupShown(group)">
      {{item.groupName}}
    </ion-item>
</ion-list>

<div class="spacer" style="width: 283.2px; height: 10px;"></div>
<ion-list>
    <ion-item class="item-stable" ng-click="toggleGroup(group)" ng-class="{active: isGroupShown(group)}">
      <i class="icon" ng-class="isGroupShown(group) ? 'ion-minus' : 'ion-plus'"></i>
      &nbsp; Category :
    </ion-item>
    <ion-item class="item-accordion" ng-repeat="item in category track by $index" ng-show="isGroupShown(group)">
      {{item.categoryName}}
    </ion-item>
</ion-list>

在我的控制器中:

 $scope.group = [];
 $scope.toggleGroup = function(group) {
if ($scope.isGroupShown(group)) {
  $scope.shownGroup = null;
} else {
  $scope.shownGroup = group;
}
};

谢谢:)

1 个答案:

答案 0 :(得分:0)

使用ng-if进行隐藏和显示&#34;没有组&#34;信息

<ion-list>
    <ion-item class="item-stable" ng-click="toggleGroup(group)" ng-class="{active: isGroupShown(group)}">
      <i class="icon" ng-class="isGroupShown(group) ? 'ion-minus' : 'ion-plus'"></i>
      &nbsp; Group 1 :
    </ion-item>
    <ion-item class="item-accordion" ng-repeat="item in group track by $index" ng-show="isGroupShown(group)">
      {{item.groupName}}
    </ion-item>
     <ion-item ng-if="group.length">  // hide an show "no Group" part
       No Group
    </ion-item>
</ion-list>

<div class="spacer" style="width: 283.2px; height: 10px;"></div>
<ion-list>
    <ion-item class="item-stable" ng-click="toggleGroup(group)" ng-class="{active: isGroupShown(group)}">
      <i class="icon" ng-class="isGroupShown(group) ? 'ion-minus' : 'ion-plus'"></i>
      &nbsp; Category :
    </ion-item>
    <ion-item class="item-accordion" ng-repeat="item in category track by $index" ng-show="isGroupShown(group)">
      {{item.categoryName}}
    </ion-item>
    <ion-item ng-if="category .length">  // hide an show "no category" part
   No category 
</ion-item>
</ion-list>