简体   繁体   English

Angular2如何仅在没有服务器数据的情况下正确使用ngIf显示某些文本?

[英]Angular2 How can I use ngIf correctly to show some text only when there's no data from server?

I am getting some data from server and using ngFor to show them (It's search feature) but when there's no result, I want to show text saying 'There's no result" 我正在从服务器获取一些数据,并使用ngFor来显示它们(这是搜索功能),但是当没有结果时,我想显示文字说“没有结果”

How can I do this? 我怎样才能做到这一点?

I tried this so far but this isn't working. 到目前为止,我已经尝试过了,但是没有用。

 <div *ngIf="teaInfo != '{}'">

        <div class="starter-template text-xs-center">
            <h5 style = "text-align:center">No result</h5>

        </div>

   </div>

Use ngSwitch to check data exist and show default message if no data available as below: 使用ngSwitch检查数据是否存在,如果没有可用数据,则显示默认消息 ,如下所示:

You can also get below result with help of IF statement but advisable to use Switch instead of If statement for better performance perspective. 您也可以在IF语句的帮助下获得以下结果,但建议使用Switch而不是If语句,以获取更好的性能。

<div [ngSwitch]="true">
    <div *ngSwitchCase="teaInfo != null && teaInfo.length>0">
        //Perform operation on data
    </div>
    <div *ngSwitchDefault>
         There's no result                 
     </div>
</div>

NgFor is used for an array. NgFor用于数组。 So just check 所以只要检查一下

*ngIf="teaInfo.length>0"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM