How to show table if it has data from api otherwise show other div The Next CEO of Stack OverflowHow do I import other TypeScript files?How to get query params from url in Angular 2?How do I pass data to Angular routed components?Angular 2/4 console.log array empty, gettting data from service is async, so it is not going to have data availableUsing *ngFor in CSS Grid Layout Undesirably Displaying Everything in One ColumnHow to retrieve nested values after subscribing using angular and json data?how to check ng-if after change eventAngular 6 UI not updatedThe data I am receiving from GET method is not being updated on html pageAngular 7 : Data looks empty

How to Reset Passwords on Multiple Websites Easily?

Can I equip Skullclamp on a creature I am sacrificing?

Example of a Mathematician/Physicist whose Other Publications during their PhD eclipsed their PhD Thesis

Apart from "berlinern", do any other German dialects have a corresponding verb?

Implement the Thanos sorting algorithm

What can we do to stop prior company from asking us questions?

Why didn't Theresa May consult with Parliament before negotiating a deal with the EU?

Which organization defines CJK Unified Ideographs?

Why do professional authors make "consistency" mistakes? And how to avoid them?

How to get regions to plot as graphics

Inappropriate reference requests from Journal reviewers

Text adventure game code

How easy is it to start Magic from scratch?

How to be diplomatic in refusing to write code that breaches the privacy of our users

Why doesn't a table tennis ball float on the surface? How do we calculate buoyancy here?

Can a single photon have an energy density?

Should I tutor a student who I know has cheated on their homework?

What is the difference between "behavior" and "behaviour"?

How to count occurrences of text in a file?

Shade part of a Venn diagram

Why does C# sound extremely flat when saxophone is tuned to G?

Why did we only see the N-1 starfighters in one film?

How do we know the LHC results are robust?

How to make a software documentation "officially" citable?



How to show table if it has data from api otherwise show other div



The Next CEO of Stack OverflowHow do I import other TypeScript files?How to get query params from url in Angular 2?How do I pass data to Angular routed components?Angular 2/4 console.log array empty, gettting data from service is async, so it is not going to have data availableUsing *ngFor in CSS Grid Layout Undesirably Displaying Everything in One ColumnHow to retrieve nested values after subscribing using angular and json data?how to check ng-if after change eventAngular 6 UI not updatedThe data I am receiving from GET method is not being updated on html pageAngular 7 : Data looks empty










0















In my Angular 6 application. I have a table showing data from a web api, also I have some ngIf containers. One shows a message if the web api data is empty. The other one should show the table if there is data coming back in. My current code does not work:



.ts file



 public errorApi = false;
public tableShow = false;

ngOnInit()
this.service.getIncidents(this.customer_id).subscribe((data) => data.result.length === 0;
this.tableShow = this.data && this.data.result;
)

}


html



<ng-container *ngIf="errorApi">
<div class="column col-12 text-center pt-10 pb-10">
<div class="empty">
<div class="empty-icon">
<i class="icon icon-people"></i>
</div>
<p class="empty-title h5">There are no incidents to display</p>
<div class="empty-action">
<button class="btn btn-primary">Create an incident</button>
</div>
</div>
</div>
</ng-container>

<ng-container *ngIf="tableshow">
<table></table>
</ng-container>









share|improve this question
























  • When you are doing this.data = data.result why are you checking this.data && this.data.result ?

    – sabithpocker
    Mar 8 at 13:22











  • If there is data or is null

    – Sole
    Mar 8 at 13:22











  • I guess you should only be checking this.tableShow = this.data as this.data already has data.result ? Or did you mean this.data.length ?

    – sabithpocker
    Mar 8 at 13:23
















0















In my Angular 6 application. I have a table showing data from a web api, also I have some ngIf containers. One shows a message if the web api data is empty. The other one should show the table if there is data coming back in. My current code does not work:



.ts file



 public errorApi = false;
public tableShow = false;

ngOnInit()
this.service.getIncidents(this.customer_id).subscribe((data) => data.result.length === 0;
this.tableShow = this.data && this.data.result;
)

}


html



<ng-container *ngIf="errorApi">
<div class="column col-12 text-center pt-10 pb-10">
<div class="empty">
<div class="empty-icon">
<i class="icon icon-people"></i>
</div>
<p class="empty-title h5">There are no incidents to display</p>
<div class="empty-action">
<button class="btn btn-primary">Create an incident</button>
</div>
</div>
</div>
</ng-container>

<ng-container *ngIf="tableshow">
<table></table>
</ng-container>









share|improve this question
























  • When you are doing this.data = data.result why are you checking this.data && this.data.result ?

    – sabithpocker
    Mar 8 at 13:22











  • If there is data or is null

    – Sole
    Mar 8 at 13:22











  • I guess you should only be checking this.tableShow = this.data as this.data already has data.result ? Or did you mean this.data.length ?

    – sabithpocker
    Mar 8 at 13:23














0












0








0


1






In my Angular 6 application. I have a table showing data from a web api, also I have some ngIf containers. One shows a message if the web api data is empty. The other one should show the table if there is data coming back in. My current code does not work:



.ts file



 public errorApi = false;
public tableShow = false;

ngOnInit()
this.service.getIncidents(this.customer_id).subscribe((data) => data.result.length === 0;
this.tableShow = this.data && this.data.result;
)

}


html



<ng-container *ngIf="errorApi">
<div class="column col-12 text-center pt-10 pb-10">
<div class="empty">
<div class="empty-icon">
<i class="icon icon-people"></i>
</div>
<p class="empty-title h5">There are no incidents to display</p>
<div class="empty-action">
<button class="btn btn-primary">Create an incident</button>
</div>
</div>
</div>
</ng-container>

<ng-container *ngIf="tableshow">
<table></table>
</ng-container>









share|improve this question
















In my Angular 6 application. I have a table showing data from a web api, also I have some ngIf containers. One shows a message if the web api data is empty. The other one should show the table if there is data coming back in. My current code does not work:



.ts file



 public errorApi = false;
public tableShow = false;

ngOnInit()
this.service.getIncidents(this.customer_id).subscribe((data) => data.result.length === 0;
this.tableShow = this.data && this.data.result;
)

}


html



<ng-container *ngIf="errorApi">
<div class="column col-12 text-center pt-10 pb-10">
<div class="empty">
<div class="empty-icon">
<i class="icon icon-people"></i>
</div>
<p class="empty-title h5">There are no incidents to display</p>
<div class="empty-action">
<button class="btn btn-primary">Create an incident</button>
</div>
</div>
</div>
</ng-container>

<ng-container *ngIf="tableshow">
<table></table>
</ng-container>






angular typescript






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 8 at 13:26









Zoe

13k85085




13k85085










asked Mar 8 at 13:12









SoleSole

4264925




4264925












  • When you are doing this.data = data.result why are you checking this.data && this.data.result ?

    – sabithpocker
    Mar 8 at 13:22











  • If there is data or is null

    – Sole
    Mar 8 at 13:22











  • I guess you should only be checking this.tableShow = this.data as this.data already has data.result ? Or did you mean this.data.length ?

    – sabithpocker
    Mar 8 at 13:23


















  • When you are doing this.data = data.result why are you checking this.data && this.data.result ?

    – sabithpocker
    Mar 8 at 13:22











  • If there is data or is null

    – Sole
    Mar 8 at 13:22











  • I guess you should only be checking this.tableShow = this.data as this.data already has data.result ? Or did you mean this.data.length ?

    – sabithpocker
    Mar 8 at 13:23

















When you are doing this.data = data.result why are you checking this.data && this.data.result ?

– sabithpocker
Mar 8 at 13:22





When you are doing this.data = data.result why are you checking this.data && this.data.result ?

– sabithpocker
Mar 8 at 13:22













If there is data or is null

– Sole
Mar 8 at 13:22





If there is data or is null

– Sole
Mar 8 at 13:22













I guess you should only be checking this.tableShow = this.data as this.data already has data.result ? Or did you mean this.data.length ?

– sabithpocker
Mar 8 at 13:23






I guess you should only be checking this.tableShow = this.data as this.data already has data.result ? Or did you mean this.data.length ?

– sabithpocker
Mar 8 at 13:23













4 Answers
4






active

oldest

votes


















1














Use the data variable directly to check if it has data or not.



This checks if you have data in the data array. If it has, it will display the table but if it's empty array or null then it will display the error.



Define your data variable in the ts first:
data = [];



<div *ngIf="!data.length > 0">
<div class="column col-12 text-center pt-10 pb-10">
<div class="empty">
<div class="empty-icon">
<i class="icon icon-people"></i>
</div>
<p class="empty-title h5">There are no incidents to display</p>
<div class="empty-action">
<button class="btn btn-primary">Create an incident</button>
</div>
</div>
</div>
</div>

<div *ngIf="data.length > 0">
<p>Table Data here</p>
</div>





share|improve this answer

























  • I get an error Cannot read property 'length' of undefined?

    – Sole
    Mar 8 at 13:28






  • 1





    @MaihanNijat in order to avoid undefined you have to initialize data while declare data = [] in ts file. Right now angular template doesnot aware if data is going to be an or []

    – mumair
    Mar 8 at 13:39






  • 1





    other good solution is async pipe

    – mumair
    Mar 8 at 13:40











  • My data is defined in the .ts file as public data: any;

    – Sole
    Mar 8 at 16:39











  • Still get that error: error Cannot read property 'length' of undefined?

    – Sole
    Mar 8 at 16:39


















2














Try else in ngIf statement



<ng-container *ngIf="errorApi else apiTable">
<div class="column col-12 text-center pt-10 pb-10">
<div class="empty">
<div class="empty-icon">
<i class="icon icon-people"></i>
</div>
<p class="empty-title h5">There are no incidents to display</p>
<div class="empty-action">
<button class="btn btn-primary">Create an incident</button>
</div>
</div>
</div>
</ng-container>

<ng-container #apiTable>
<table></table>
</ng-container>





share|improve this answer






























    1














    Try else from *ngIf directive:



    *ngIf="errorApi; else apiTable"





    share|improve this answer

























    • In the original question there is no "apiTable"

      – A. Roussos
      Mar 8 at 13:46


















    0














    public data: any;
    public loading: boolean;

    private subscription: Subscription;

    ngOnInit()
    this.loading = true;
    this.subscription = this.service.getIncidents(this.customer_id)
    .subscribe((data: any) =>
    this.loading = false;

    // Because you have no error logic in your service
    If (data.result //&& any other validation on the data: )
    this.data = data.result;

    console.log('Result - ', data);
    console.log('data is received');
    else
    this.errorApi = true;

    );


    ngOnDestroy(): void
    this.subscription.unsubscribe();



    And in your html you can use ng-template:



    <ng-template *ngIf="data && !loading; else errorTemplate">...</ng-template>
    <ng-template #errorTemplate>...</ng-template>


    Obviously, accommodate for your validation on the data: but this should provide you some ideas.






    share|improve this answer























    • Ideally you should implement your service to return the data as valid and use (error: any) => to account for the errors you may encounter. That way you don't have to check twice for the data and you can simply use this.data as a check in your DOM as oppose to all the other conditions on the object you are currently using.

      – mzaleski
      Mar 8 at 13:44











    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55063960%2fhow-to-show-table-if-it-has-data-from-api-otherwise-show-other-div%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    4 Answers
    4






    active

    oldest

    votes








    4 Answers
    4






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Use the data variable directly to check if it has data or not.



    This checks if you have data in the data array. If it has, it will display the table but if it's empty array or null then it will display the error.



    Define your data variable in the ts first:
    data = [];



    <div *ngIf="!data.length > 0">
    <div class="column col-12 text-center pt-10 pb-10">
    <div class="empty">
    <div class="empty-icon">
    <i class="icon icon-people"></i>
    </div>
    <p class="empty-title h5">There are no incidents to display</p>
    <div class="empty-action">
    <button class="btn btn-primary">Create an incident</button>
    </div>
    </div>
    </div>
    </div>

    <div *ngIf="data.length > 0">
    <p>Table Data here</p>
    </div>





    share|improve this answer

























    • I get an error Cannot read property 'length' of undefined?

      – Sole
      Mar 8 at 13:28






    • 1





      @MaihanNijat in order to avoid undefined you have to initialize data while declare data = [] in ts file. Right now angular template doesnot aware if data is going to be an or []

      – mumair
      Mar 8 at 13:39






    • 1





      other good solution is async pipe

      – mumair
      Mar 8 at 13:40











    • My data is defined in the .ts file as public data: any;

      – Sole
      Mar 8 at 16:39











    • Still get that error: error Cannot read property 'length' of undefined?

      – Sole
      Mar 8 at 16:39















    1














    Use the data variable directly to check if it has data or not.



    This checks if you have data in the data array. If it has, it will display the table but if it's empty array or null then it will display the error.



    Define your data variable in the ts first:
    data = [];



    <div *ngIf="!data.length > 0">
    <div class="column col-12 text-center pt-10 pb-10">
    <div class="empty">
    <div class="empty-icon">
    <i class="icon icon-people"></i>
    </div>
    <p class="empty-title h5">There are no incidents to display</p>
    <div class="empty-action">
    <button class="btn btn-primary">Create an incident</button>
    </div>
    </div>
    </div>
    </div>

    <div *ngIf="data.length > 0">
    <p>Table Data here</p>
    </div>





    share|improve this answer

























    • I get an error Cannot read property 'length' of undefined?

      – Sole
      Mar 8 at 13:28






    • 1





      @MaihanNijat in order to avoid undefined you have to initialize data while declare data = [] in ts file. Right now angular template doesnot aware if data is going to be an or []

      – mumair
      Mar 8 at 13:39






    • 1





      other good solution is async pipe

      – mumair
      Mar 8 at 13:40











    • My data is defined in the .ts file as public data: any;

      – Sole
      Mar 8 at 16:39











    • Still get that error: error Cannot read property 'length' of undefined?

      – Sole
      Mar 8 at 16:39













    1












    1








    1







    Use the data variable directly to check if it has data or not.



    This checks if you have data in the data array. If it has, it will display the table but if it's empty array or null then it will display the error.



    Define your data variable in the ts first:
    data = [];



    <div *ngIf="!data.length > 0">
    <div class="column col-12 text-center pt-10 pb-10">
    <div class="empty">
    <div class="empty-icon">
    <i class="icon icon-people"></i>
    </div>
    <p class="empty-title h5">There are no incidents to display</p>
    <div class="empty-action">
    <button class="btn btn-primary">Create an incident</button>
    </div>
    </div>
    </div>
    </div>

    <div *ngIf="data.length > 0">
    <p>Table Data here</p>
    </div>





    share|improve this answer















    Use the data variable directly to check if it has data or not.



    This checks if you have data in the data array. If it has, it will display the table but if it's empty array or null then it will display the error.



    Define your data variable in the ts first:
    data = [];



    <div *ngIf="!data.length > 0">
    <div class="column col-12 text-center pt-10 pb-10">
    <div class="empty">
    <div class="empty-icon">
    <i class="icon icon-people"></i>
    </div>
    <p class="empty-title h5">There are no incidents to display</p>
    <div class="empty-action">
    <button class="btn btn-primary">Create an incident</button>
    </div>
    </div>
    </div>
    </div>

    <div *ngIf="data.length > 0">
    <p>Table Data here</p>
    </div>






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Mar 8 at 17:06

























    answered Mar 8 at 13:18









    Maihan NijatMaihan Nijat

    2,94822448




    2,94822448












    • I get an error Cannot read property 'length' of undefined?

      – Sole
      Mar 8 at 13:28






    • 1





      @MaihanNijat in order to avoid undefined you have to initialize data while declare data = [] in ts file. Right now angular template doesnot aware if data is going to be an or []

      – mumair
      Mar 8 at 13:39






    • 1





      other good solution is async pipe

      – mumair
      Mar 8 at 13:40











    • My data is defined in the .ts file as public data: any;

      – Sole
      Mar 8 at 16:39











    • Still get that error: error Cannot read property 'length' of undefined?

      – Sole
      Mar 8 at 16:39

















    • I get an error Cannot read property 'length' of undefined?

      – Sole
      Mar 8 at 13:28






    • 1





      @MaihanNijat in order to avoid undefined you have to initialize data while declare data = [] in ts file. Right now angular template doesnot aware if data is going to be an or []

      – mumair
      Mar 8 at 13:39






    • 1





      other good solution is async pipe

      – mumair
      Mar 8 at 13:40











    • My data is defined in the .ts file as public data: any;

      – Sole
      Mar 8 at 16:39











    • Still get that error: error Cannot read property 'length' of undefined?

      – Sole
      Mar 8 at 16:39
















    I get an error Cannot read property 'length' of undefined?

    – Sole
    Mar 8 at 13:28





    I get an error Cannot read property 'length' of undefined?

    – Sole
    Mar 8 at 13:28




    1




    1





    @MaihanNijat in order to avoid undefined you have to initialize data while declare data = [] in ts file. Right now angular template doesnot aware if data is going to be an or []

    – mumair
    Mar 8 at 13:39





    @MaihanNijat in order to avoid undefined you have to initialize data while declare data = [] in ts file. Right now angular template doesnot aware if data is going to be an or []

    – mumair
    Mar 8 at 13:39




    1




    1





    other good solution is async pipe

    – mumair
    Mar 8 at 13:40





    other good solution is async pipe

    – mumair
    Mar 8 at 13:40













    My data is defined in the .ts file as public data: any;

    – Sole
    Mar 8 at 16:39





    My data is defined in the .ts file as public data: any;

    – Sole
    Mar 8 at 16:39













    Still get that error: error Cannot read property 'length' of undefined?

    – Sole
    Mar 8 at 16:39





    Still get that error: error Cannot read property 'length' of undefined?

    – Sole
    Mar 8 at 16:39













    2














    Try else in ngIf statement



    <ng-container *ngIf="errorApi else apiTable">
    <div class="column col-12 text-center pt-10 pb-10">
    <div class="empty">
    <div class="empty-icon">
    <i class="icon icon-people"></i>
    </div>
    <p class="empty-title h5">There are no incidents to display</p>
    <div class="empty-action">
    <button class="btn btn-primary">Create an incident</button>
    </div>
    </div>
    </div>
    </ng-container>

    <ng-container #apiTable>
    <table></table>
    </ng-container>





    share|improve this answer



























      2














      Try else in ngIf statement



      <ng-container *ngIf="errorApi else apiTable">
      <div class="column col-12 text-center pt-10 pb-10">
      <div class="empty">
      <div class="empty-icon">
      <i class="icon icon-people"></i>
      </div>
      <p class="empty-title h5">There are no incidents to display</p>
      <div class="empty-action">
      <button class="btn btn-primary">Create an incident</button>
      </div>
      </div>
      </div>
      </ng-container>

      <ng-container #apiTable>
      <table></table>
      </ng-container>





      share|improve this answer

























        2












        2








        2







        Try else in ngIf statement



        <ng-container *ngIf="errorApi else apiTable">
        <div class="column col-12 text-center pt-10 pb-10">
        <div class="empty">
        <div class="empty-icon">
        <i class="icon icon-people"></i>
        </div>
        <p class="empty-title h5">There are no incidents to display</p>
        <div class="empty-action">
        <button class="btn btn-primary">Create an incident</button>
        </div>
        </div>
        </div>
        </ng-container>

        <ng-container #apiTable>
        <table></table>
        </ng-container>





        share|improve this answer













        Try else in ngIf statement



        <ng-container *ngIf="errorApi else apiTable">
        <div class="column col-12 text-center pt-10 pb-10">
        <div class="empty">
        <div class="empty-icon">
        <i class="icon icon-people"></i>
        </div>
        <p class="empty-title h5">There are no incidents to display</p>
        <div class="empty-action">
        <button class="btn btn-primary">Create an incident</button>
        </div>
        </div>
        </div>
        </ng-container>

        <ng-container #apiTable>
        <table></table>
        </ng-container>






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 8 at 13:19









        A. RoussosA. Roussos

        17719




        17719





















            1














            Try else from *ngIf directive:



            *ngIf="errorApi; else apiTable"





            share|improve this answer

























            • In the original question there is no "apiTable"

              – A. Roussos
              Mar 8 at 13:46















            1














            Try else from *ngIf directive:



            *ngIf="errorApi; else apiTable"





            share|improve this answer

























            • In the original question there is no "apiTable"

              – A. Roussos
              Mar 8 at 13:46













            1












            1








            1







            Try else from *ngIf directive:



            *ngIf="errorApi; else apiTable"





            share|improve this answer















            Try else from *ngIf directive:



            *ngIf="errorApi; else apiTable"






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Mar 8 at 14:22









            mumair

            1,6171934




            1,6171934










            answered Mar 8 at 13:21









            Yuri KevinYuri Kevin

            212




            212












            • In the original question there is no "apiTable"

              – A. Roussos
              Mar 8 at 13:46

















            • In the original question there is no "apiTable"

              – A. Roussos
              Mar 8 at 13:46
















            In the original question there is no "apiTable"

            – A. Roussos
            Mar 8 at 13:46





            In the original question there is no "apiTable"

            – A. Roussos
            Mar 8 at 13:46











            0














            public data: any;
            public loading: boolean;

            private subscription: Subscription;

            ngOnInit()
            this.loading = true;
            this.subscription = this.service.getIncidents(this.customer_id)
            .subscribe((data: any) =>
            this.loading = false;

            // Because you have no error logic in your service
            If (data.result //&& any other validation on the data: )
            this.data = data.result;

            console.log('Result - ', data);
            console.log('data is received');
            else
            this.errorApi = true;

            );


            ngOnDestroy(): void
            this.subscription.unsubscribe();



            And in your html you can use ng-template:



            <ng-template *ngIf="data && !loading; else errorTemplate">...</ng-template>
            <ng-template #errorTemplate>...</ng-template>


            Obviously, accommodate for your validation on the data: but this should provide you some ideas.






            share|improve this answer























            • Ideally you should implement your service to return the data as valid and use (error: any) => to account for the errors you may encounter. That way you don't have to check twice for the data and you can simply use this.data as a check in your DOM as oppose to all the other conditions on the object you are currently using.

              – mzaleski
              Mar 8 at 13:44















            0














            public data: any;
            public loading: boolean;

            private subscription: Subscription;

            ngOnInit()
            this.loading = true;
            this.subscription = this.service.getIncidents(this.customer_id)
            .subscribe((data: any) =>
            this.loading = false;

            // Because you have no error logic in your service
            If (data.result //&& any other validation on the data: )
            this.data = data.result;

            console.log('Result - ', data);
            console.log('data is received');
            else
            this.errorApi = true;

            );


            ngOnDestroy(): void
            this.subscription.unsubscribe();



            And in your html you can use ng-template:



            <ng-template *ngIf="data && !loading; else errorTemplate">...</ng-template>
            <ng-template #errorTemplate>...</ng-template>


            Obviously, accommodate for your validation on the data: but this should provide you some ideas.






            share|improve this answer























            • Ideally you should implement your service to return the data as valid and use (error: any) => to account for the errors you may encounter. That way you don't have to check twice for the data and you can simply use this.data as a check in your DOM as oppose to all the other conditions on the object you are currently using.

              – mzaleski
              Mar 8 at 13:44













            0












            0








            0







            public data: any;
            public loading: boolean;

            private subscription: Subscription;

            ngOnInit()
            this.loading = true;
            this.subscription = this.service.getIncidents(this.customer_id)
            .subscribe((data: any) =>
            this.loading = false;

            // Because you have no error logic in your service
            If (data.result //&& any other validation on the data: )
            this.data = data.result;

            console.log('Result - ', data);
            console.log('data is received');
            else
            this.errorApi = true;

            );


            ngOnDestroy(): void
            this.subscription.unsubscribe();



            And in your html you can use ng-template:



            <ng-template *ngIf="data && !loading; else errorTemplate">...</ng-template>
            <ng-template #errorTemplate>...</ng-template>


            Obviously, accommodate for your validation on the data: but this should provide you some ideas.






            share|improve this answer













            public data: any;
            public loading: boolean;

            private subscription: Subscription;

            ngOnInit()
            this.loading = true;
            this.subscription = this.service.getIncidents(this.customer_id)
            .subscribe((data: any) =>
            this.loading = false;

            // Because you have no error logic in your service
            If (data.result //&& any other validation on the data: )
            this.data = data.result;

            console.log('Result - ', data);
            console.log('data is received');
            else
            this.errorApi = true;

            );


            ngOnDestroy(): void
            this.subscription.unsubscribe();



            And in your html you can use ng-template:



            <ng-template *ngIf="data && !loading; else errorTemplate">...</ng-template>
            <ng-template #errorTemplate>...</ng-template>


            Obviously, accommodate for your validation on the data: but this should provide you some ideas.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 8 at 13:37









            mzaleskimzaleski

            135




            135












            • Ideally you should implement your service to return the data as valid and use (error: any) => to account for the errors you may encounter. That way you don't have to check twice for the data and you can simply use this.data as a check in your DOM as oppose to all the other conditions on the object you are currently using.

              – mzaleski
              Mar 8 at 13:44

















            • Ideally you should implement your service to return the data as valid and use (error: any) => to account for the errors you may encounter. That way you don't have to check twice for the data and you can simply use this.data as a check in your DOM as oppose to all the other conditions on the object you are currently using.

              – mzaleski
              Mar 8 at 13:44
















            Ideally you should implement your service to return the data as valid and use (error: any) => to account for the errors you may encounter. That way you don't have to check twice for the data and you can simply use this.data as a check in your DOM as oppose to all the other conditions on the object you are currently using.

            – mzaleski
            Mar 8 at 13:44





            Ideally you should implement your service to return the data as valid and use (error: any) => to account for the errors you may encounter. That way you don't have to check twice for the data and you can simply use this.data as a check in your DOM as oppose to all the other conditions on the object you are currently using.

            – mzaleski
            Mar 8 at 13:44

















            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55063960%2fhow-to-show-table-if-it-has-data-from-api-otherwise-show-other-div%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

            2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived

            Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme