【问题标题】:Show Message if results are empty如果结果为空,则显示消息
【发布时间】:2017-09-28 22:07:34
【问题描述】:
我有一个产品搜索表单。我想确保它在列表为空、没有结果时向我显示一条消息。
我的代码:
<form ng-controller="productCtrl" name="searchForm" ng-submit="searchProduct()" ng-disabled="searchForm.$invalid">
<input placeholder="Search" type="text" ng-model="form.name" required>
<ion-list>
<ion-item ng-repeat="product in list track by $index">{{ product.name }}</ion-item>
</ion-list>
</form>
谢谢你:)
【问题讨论】:
标签:
angularjs
forms
ionic-framework
【解决方案1】:
有很多不同的答案,例如:
在另一个显示空状态消息的元素中使用 *ng-if** 或 ng-show。检查将显示的列表的长度。如果list.length <= 0 显示此错误。
在 ng-if 元素中也使用 ng-if 或 ng-hide 添加条件。如果list.length <=0.
或者您可以在您的作用域对象中创建布尔标志,例如 isShowEmptyState=false 并在列表为空时设置 true
【解决方案2】:
您可以在 ng-repeat
上方使用
ng-if
这样
<div ng-if="list.length<=0" style="color:red;">Error!</div>
例如:
<div ng-app="app">
<div ng-controller="TodoCtrl">
<div ng-if="todos.length<=0" style="color:red;">Error!</div>
<div ng-repeat="item in todos">{{item.color}}</div>
</div>
</div>
https://jsfiddle.net/vqJ5q/48/