Angular 6 *ngFor returns empty array while array it's actually full










0














I'm currently developing a simple chat (like Facebook's) for a web application.
I'm using Angular 6 as framework and Firebase as my database.



When a user clicks on a certain conversation, I'm retrieving the previous messages using the .on("child_added, //function here) and then I'm pushing the results into an array which will be displayed using an *ngFor loop.



The weird issue I'm encountering is that the messages will be displayed sometimes correctly, and sometimes not displayed at all. When this happens, as soon as I click anywhere, the *ngFor will be called again and everything looks fine.



Using console.logs I found out that the data is pushed correctly into the array every time, but, for some reason, the ngFor (same with ngAfterViewChecked) sometimes will update and sometimes will just return an empty array.



My .ts file (relevant parts):



ngAfterViewChecked() 
/*
when everything works, this will be called everytime something changes
(even after the array gets populated)
when the issue comes up, this will be called as usual but it won't be called after the array gets populated,
thus returning an empty array the last time it was called
*/

console.log(this.msgService.messageList)


//this will be called everytime a user clicks on a conversation on the left sidebar
getSpecConversation(conversation)
//emptying the array
this.msgService.messageList = ;

//calling the .off using previous conversation key
this.msgService.getConversation(this.msgService.selectedConversation.$key)
.off('child_added');

//getting messages of currently selected conversation
this.msgService.getConversation(conversation.$key)
.on('child_added', (data =>
//push the data into array
this.msgService.messageList.push(data.val());
)
);

//storing currently selected conversation
this.msgService.selectedConversation = conversation;



the Template (relevant parts):



<ul class="conversation-list">
<li *ngFor="let message of msgService.messageList" class="conversation-item" [class.sender]="message.byUid == userInfoService.userUid">
<p class="message">message.msg</p>
<p class="msg-time">
<span> date: 'shortTime'</span></p>
</li>
</ul>


The Service:



getConversation(sid: string) 
return this.database.ref('/messages')



Thanks for the help



Update
If it helps, if I delete the .off('child_added') method, the problem won't show up anymore, but, when I send a new message, I would have the new message called multiple times in the view (not in database).










share|improve this question




























    0














    I'm currently developing a simple chat (like Facebook's) for a web application.
    I'm using Angular 6 as framework and Firebase as my database.



    When a user clicks on a certain conversation, I'm retrieving the previous messages using the .on("child_added, //function here) and then I'm pushing the results into an array which will be displayed using an *ngFor loop.



    The weird issue I'm encountering is that the messages will be displayed sometimes correctly, and sometimes not displayed at all. When this happens, as soon as I click anywhere, the *ngFor will be called again and everything looks fine.



    Using console.logs I found out that the data is pushed correctly into the array every time, but, for some reason, the ngFor (same with ngAfterViewChecked) sometimes will update and sometimes will just return an empty array.



    My .ts file (relevant parts):



    ngAfterViewChecked() 
    /*
    when everything works, this will be called everytime something changes
    (even after the array gets populated)
    when the issue comes up, this will be called as usual but it won't be called after the array gets populated,
    thus returning an empty array the last time it was called
    */

    console.log(this.msgService.messageList)


    //this will be called everytime a user clicks on a conversation on the left sidebar
    getSpecConversation(conversation)
    //emptying the array
    this.msgService.messageList = ;

    //calling the .off using previous conversation key
    this.msgService.getConversation(this.msgService.selectedConversation.$key)
    .off('child_added');

    //getting messages of currently selected conversation
    this.msgService.getConversation(conversation.$key)
    .on('child_added', (data =>
    //push the data into array
    this.msgService.messageList.push(data.val());
    )
    );

    //storing currently selected conversation
    this.msgService.selectedConversation = conversation;



    the Template (relevant parts):



    <ul class="conversation-list">
    <li *ngFor="let message of msgService.messageList" class="conversation-item" [class.sender]="message.byUid == userInfoService.userUid">
    <p class="message">message.msg</p>
    <p class="msg-time">
    <span> date: 'shortTime'</span></p>
    </li>
    </ul>


    The Service:



    getConversation(sid: string) 
    return this.database.ref('/messages')



    Thanks for the help



    Update
    If it helps, if I delete the .off('child_added') method, the problem won't show up anymore, but, when I send a new message, I would have the new message called multiple times in the view (not in database).










    share|improve this question


























      0












      0








      0







      I'm currently developing a simple chat (like Facebook's) for a web application.
      I'm using Angular 6 as framework and Firebase as my database.



      When a user clicks on a certain conversation, I'm retrieving the previous messages using the .on("child_added, //function here) and then I'm pushing the results into an array which will be displayed using an *ngFor loop.



      The weird issue I'm encountering is that the messages will be displayed sometimes correctly, and sometimes not displayed at all. When this happens, as soon as I click anywhere, the *ngFor will be called again and everything looks fine.



      Using console.logs I found out that the data is pushed correctly into the array every time, but, for some reason, the ngFor (same with ngAfterViewChecked) sometimes will update and sometimes will just return an empty array.



      My .ts file (relevant parts):



      ngAfterViewChecked() 
      /*
      when everything works, this will be called everytime something changes
      (even after the array gets populated)
      when the issue comes up, this will be called as usual but it won't be called after the array gets populated,
      thus returning an empty array the last time it was called
      */

      console.log(this.msgService.messageList)


      //this will be called everytime a user clicks on a conversation on the left sidebar
      getSpecConversation(conversation)
      //emptying the array
      this.msgService.messageList = ;

      //calling the .off using previous conversation key
      this.msgService.getConversation(this.msgService.selectedConversation.$key)
      .off('child_added');

      //getting messages of currently selected conversation
      this.msgService.getConversation(conversation.$key)
      .on('child_added', (data =>
      //push the data into array
      this.msgService.messageList.push(data.val());
      )
      );

      //storing currently selected conversation
      this.msgService.selectedConversation = conversation;



      the Template (relevant parts):



      <ul class="conversation-list">
      <li *ngFor="let message of msgService.messageList" class="conversation-item" [class.sender]="message.byUid == userInfoService.userUid">
      <p class="message">message.msg</p>
      <p class="msg-time">
      <span> date: 'shortTime'</span></p>
      </li>
      </ul>


      The Service:



      getConversation(sid: string) 
      return this.database.ref('/messages')



      Thanks for the help



      Update
      If it helps, if I delete the .off('child_added') method, the problem won't show up anymore, but, when I send a new message, I would have the new message called multiple times in the view (not in database).










      share|improve this question















      I'm currently developing a simple chat (like Facebook's) for a web application.
      I'm using Angular 6 as framework and Firebase as my database.



      When a user clicks on a certain conversation, I'm retrieving the previous messages using the .on("child_added, //function here) and then I'm pushing the results into an array which will be displayed using an *ngFor loop.



      The weird issue I'm encountering is that the messages will be displayed sometimes correctly, and sometimes not displayed at all. When this happens, as soon as I click anywhere, the *ngFor will be called again and everything looks fine.



      Using console.logs I found out that the data is pushed correctly into the array every time, but, for some reason, the ngFor (same with ngAfterViewChecked) sometimes will update and sometimes will just return an empty array.



      My .ts file (relevant parts):



      ngAfterViewChecked() 
      /*
      when everything works, this will be called everytime something changes
      (even after the array gets populated)
      when the issue comes up, this will be called as usual but it won't be called after the array gets populated,
      thus returning an empty array the last time it was called
      */

      console.log(this.msgService.messageList)


      //this will be called everytime a user clicks on a conversation on the left sidebar
      getSpecConversation(conversation)
      //emptying the array
      this.msgService.messageList = ;

      //calling the .off using previous conversation key
      this.msgService.getConversation(this.msgService.selectedConversation.$key)
      .off('child_added');

      //getting messages of currently selected conversation
      this.msgService.getConversation(conversation.$key)
      .on('child_added', (data =>
      //push the data into array
      this.msgService.messageList.push(data.val());
      )
      );

      //storing currently selected conversation
      this.msgService.selectedConversation = conversation;



      the Template (relevant parts):



      <ul class="conversation-list">
      <li *ngFor="let message of msgService.messageList" class="conversation-item" [class.sender]="message.byUid == userInfoService.userUid">
      <p class="message">message.msg</p>
      <p class="msg-time">
      <span> date: 'shortTime'</span></p>
      </li>
      </ul>


      The Service:



      getConversation(sid: string) 
      return this.database.ref('/messages')



      Thanks for the help



      Update
      If it helps, if I delete the .off('child_added') method, the problem won't show up anymore, but, when I send a new message, I would have the new message called multiple times in the view (not in database).







      firebase angular6 ngfor






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 0:10







      F. Rusconi

















      asked Nov 12 '18 at 23:16









      F. RusconiF. Rusconi

      266




      266






















          2 Answers
          2






          active

          oldest

          votes


















          0














          Update your template and check that the array has data before you show it.



          <ul class="conversation-list" *ngIf="msgService.messageList && msgService.messageList.length > 0">
          <li *ngFor="let message of msgService.messageList" class="conversation-item" [class.sender]="message.byUid == userInfoService.userUid">
          <p class="message">message.msg</p>
          <p class="msg-time">
          <span> date: 'shortTime'</span></p>
          </li>
          </ul>


          It could be a race condition - the ngFor loop could run before the array has any data in it, and then when you click around the page, a hook is running telling the ngFor to run again - this time finding data.



          When you run the ngIf before it will check for data, as soon as it's available it allows the ngFor to run which will now have data to iterate over and display in your list.






          share|improve this answer




















          • Thanks for your answer. I updated the template as suggested but the problem is still the same. I noticed that the error occur when I select a conversation, then select a different one and then select the first again. Though this is not always the case, seems to be the majority of the times. Thanks again
            – F. Rusconi
            Nov 12 '18 at 23:43










          • So, theoretically, you're answer seems to be correct. If I add a dummy <p *ngIf="!msgService.messageList || msgService.messageList.length === 0">nothing here</p> this will be visible for a very small time in between the change of conversations. When the problem occurs, the "nothing here" will be seen constantly. Once again, only when I do something, then it will show me the list of messages
            – F. Rusconi
            Nov 13 '18 at 0:29










          • That's strange - if you build your app in production ng b --prod and run does it work? Is there a bug with a lifecycle hook not firing correctly?
            – sketchthat
            Nov 13 '18 at 0:49


















          0














          You're getting the data from a service, so it could be that you need to use the async pipe.



          <li *ngFor="let message of msgService.messageList | async">
          <p class="message">message.msg</p>
          </li>





          share|improve this answer




















          • Message list isn't an observable / promise it is a static array so this won't work.
            – sketchthat
            Nov 13 '18 at 0:47






          • 1




            @sketchthat Ah yes you're right. I always used something like this to prevent this type of error. *ngIf="msgService.messageList?.length > 0 && !isLoading" while isLoading would change depending on the status.
            – ams
            Nov 13 '18 at 1:14











          • @ams Thanks for your answer. as sketchthat correctly said, messageList is a static array. Anyway, I tried something similar to what you said in the comment above, with a isLoading that will control a progress spinner. When user clicks on conversation, isLoading will become true, thus showing only the spinner. Just underneath the part of code where I'm pushing the results to the array, I'm setting isLoading to false to hide the spinner and show the results. Same behavior, would work perfectly sometimes and do issue some others.
            – F. Rusconi
            Nov 13 '18 at 6:07










          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%2f53271482%2fangular-6-ngfor-returns-empty-array-while-array-its-actually-full%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Update your template and check that the array has data before you show it.



          <ul class="conversation-list" *ngIf="msgService.messageList && msgService.messageList.length > 0">
          <li *ngFor="let message of msgService.messageList" class="conversation-item" [class.sender]="message.byUid == userInfoService.userUid">
          <p class="message">message.msg</p>
          <p class="msg-time">
          <span> date: 'shortTime'</span></p>
          </li>
          </ul>


          It could be a race condition - the ngFor loop could run before the array has any data in it, and then when you click around the page, a hook is running telling the ngFor to run again - this time finding data.



          When you run the ngIf before it will check for data, as soon as it's available it allows the ngFor to run which will now have data to iterate over and display in your list.






          share|improve this answer




















          • Thanks for your answer. I updated the template as suggested but the problem is still the same. I noticed that the error occur when I select a conversation, then select a different one and then select the first again. Though this is not always the case, seems to be the majority of the times. Thanks again
            – F. Rusconi
            Nov 12 '18 at 23:43










          • So, theoretically, you're answer seems to be correct. If I add a dummy <p *ngIf="!msgService.messageList || msgService.messageList.length === 0">nothing here</p> this will be visible for a very small time in between the change of conversations. When the problem occurs, the "nothing here" will be seen constantly. Once again, only when I do something, then it will show me the list of messages
            – F. Rusconi
            Nov 13 '18 at 0:29










          • That's strange - if you build your app in production ng b --prod and run does it work? Is there a bug with a lifecycle hook not firing correctly?
            – sketchthat
            Nov 13 '18 at 0:49















          0














          Update your template and check that the array has data before you show it.



          <ul class="conversation-list" *ngIf="msgService.messageList && msgService.messageList.length > 0">
          <li *ngFor="let message of msgService.messageList" class="conversation-item" [class.sender]="message.byUid == userInfoService.userUid">
          <p class="message">message.msg</p>
          <p class="msg-time">
          <span> date: 'shortTime'</span></p>
          </li>
          </ul>


          It could be a race condition - the ngFor loop could run before the array has any data in it, and then when you click around the page, a hook is running telling the ngFor to run again - this time finding data.



          When you run the ngIf before it will check for data, as soon as it's available it allows the ngFor to run which will now have data to iterate over and display in your list.






          share|improve this answer




















          • Thanks for your answer. I updated the template as suggested but the problem is still the same. I noticed that the error occur when I select a conversation, then select a different one and then select the first again. Though this is not always the case, seems to be the majority of the times. Thanks again
            – F. Rusconi
            Nov 12 '18 at 23:43










          • So, theoretically, you're answer seems to be correct. If I add a dummy <p *ngIf="!msgService.messageList || msgService.messageList.length === 0">nothing here</p> this will be visible for a very small time in between the change of conversations. When the problem occurs, the "nothing here" will be seen constantly. Once again, only when I do something, then it will show me the list of messages
            – F. Rusconi
            Nov 13 '18 at 0:29










          • That's strange - if you build your app in production ng b --prod and run does it work? Is there a bug with a lifecycle hook not firing correctly?
            – sketchthat
            Nov 13 '18 at 0:49













          0












          0








          0






          Update your template and check that the array has data before you show it.



          <ul class="conversation-list" *ngIf="msgService.messageList && msgService.messageList.length > 0">
          <li *ngFor="let message of msgService.messageList" class="conversation-item" [class.sender]="message.byUid == userInfoService.userUid">
          <p class="message">message.msg</p>
          <p class="msg-time">
          <span> date: 'shortTime'</span></p>
          </li>
          </ul>


          It could be a race condition - the ngFor loop could run before the array has any data in it, and then when you click around the page, a hook is running telling the ngFor to run again - this time finding data.



          When you run the ngIf before it will check for data, as soon as it's available it allows the ngFor to run which will now have data to iterate over and display in your list.






          share|improve this answer












          Update your template and check that the array has data before you show it.



          <ul class="conversation-list" *ngIf="msgService.messageList && msgService.messageList.length > 0">
          <li *ngFor="let message of msgService.messageList" class="conversation-item" [class.sender]="message.byUid == userInfoService.userUid">
          <p class="message">message.msg</p>
          <p class="msg-time">
          <span> date: 'shortTime'</span></p>
          </li>
          </ul>


          It could be a race condition - the ngFor loop could run before the array has any data in it, and then when you click around the page, a hook is running telling the ngFor to run again - this time finding data.



          When you run the ngIf before it will check for data, as soon as it's available it allows the ngFor to run which will now have data to iterate over and display in your list.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 12 '18 at 23:34









          sketchthatsketchthat

          1,8731818




          1,8731818











          • Thanks for your answer. I updated the template as suggested but the problem is still the same. I noticed that the error occur when I select a conversation, then select a different one and then select the first again. Though this is not always the case, seems to be the majority of the times. Thanks again
            – F. Rusconi
            Nov 12 '18 at 23:43










          • So, theoretically, you're answer seems to be correct. If I add a dummy <p *ngIf="!msgService.messageList || msgService.messageList.length === 0">nothing here</p> this will be visible for a very small time in between the change of conversations. When the problem occurs, the "nothing here" will be seen constantly. Once again, only when I do something, then it will show me the list of messages
            – F. Rusconi
            Nov 13 '18 at 0:29










          • That's strange - if you build your app in production ng b --prod and run does it work? Is there a bug with a lifecycle hook not firing correctly?
            – sketchthat
            Nov 13 '18 at 0:49
















          • Thanks for your answer. I updated the template as suggested but the problem is still the same. I noticed that the error occur when I select a conversation, then select a different one and then select the first again. Though this is not always the case, seems to be the majority of the times. Thanks again
            – F. Rusconi
            Nov 12 '18 at 23:43










          • So, theoretically, you're answer seems to be correct. If I add a dummy <p *ngIf="!msgService.messageList || msgService.messageList.length === 0">nothing here</p> this will be visible for a very small time in between the change of conversations. When the problem occurs, the "nothing here" will be seen constantly. Once again, only when I do something, then it will show me the list of messages
            – F. Rusconi
            Nov 13 '18 at 0:29










          • That's strange - if you build your app in production ng b --prod and run does it work? Is there a bug with a lifecycle hook not firing correctly?
            – sketchthat
            Nov 13 '18 at 0:49















          Thanks for your answer. I updated the template as suggested but the problem is still the same. I noticed that the error occur when I select a conversation, then select a different one and then select the first again. Though this is not always the case, seems to be the majority of the times. Thanks again
          – F. Rusconi
          Nov 12 '18 at 23:43




          Thanks for your answer. I updated the template as suggested but the problem is still the same. I noticed that the error occur when I select a conversation, then select a different one and then select the first again. Though this is not always the case, seems to be the majority of the times. Thanks again
          – F. Rusconi
          Nov 12 '18 at 23:43












          So, theoretically, you're answer seems to be correct. If I add a dummy <p *ngIf="!msgService.messageList || msgService.messageList.length === 0">nothing here</p> this will be visible for a very small time in between the change of conversations. When the problem occurs, the "nothing here" will be seen constantly. Once again, only when I do something, then it will show me the list of messages
          – F. Rusconi
          Nov 13 '18 at 0:29




          So, theoretically, you're answer seems to be correct. If I add a dummy <p *ngIf="!msgService.messageList || msgService.messageList.length === 0">nothing here</p> this will be visible for a very small time in between the change of conversations. When the problem occurs, the "nothing here" will be seen constantly. Once again, only when I do something, then it will show me the list of messages
          – F. Rusconi
          Nov 13 '18 at 0:29












          That's strange - if you build your app in production ng b --prod and run does it work? Is there a bug with a lifecycle hook not firing correctly?
          – sketchthat
          Nov 13 '18 at 0:49




          That's strange - if you build your app in production ng b --prod and run does it work? Is there a bug with a lifecycle hook not firing correctly?
          – sketchthat
          Nov 13 '18 at 0:49













          0














          You're getting the data from a service, so it could be that you need to use the async pipe.



          <li *ngFor="let message of msgService.messageList | async">
          <p class="message">message.msg</p>
          </li>





          share|improve this answer




















          • Message list isn't an observable / promise it is a static array so this won't work.
            – sketchthat
            Nov 13 '18 at 0:47






          • 1




            @sketchthat Ah yes you're right. I always used something like this to prevent this type of error. *ngIf="msgService.messageList?.length > 0 && !isLoading" while isLoading would change depending on the status.
            – ams
            Nov 13 '18 at 1:14











          • @ams Thanks for your answer. as sketchthat correctly said, messageList is a static array. Anyway, I tried something similar to what you said in the comment above, with a isLoading that will control a progress spinner. When user clicks on conversation, isLoading will become true, thus showing only the spinner. Just underneath the part of code where I'm pushing the results to the array, I'm setting isLoading to false to hide the spinner and show the results. Same behavior, would work perfectly sometimes and do issue some others.
            – F. Rusconi
            Nov 13 '18 at 6:07















          0














          You're getting the data from a service, so it could be that you need to use the async pipe.



          <li *ngFor="let message of msgService.messageList | async">
          <p class="message">message.msg</p>
          </li>





          share|improve this answer




















          • Message list isn't an observable / promise it is a static array so this won't work.
            – sketchthat
            Nov 13 '18 at 0:47






          • 1




            @sketchthat Ah yes you're right. I always used something like this to prevent this type of error. *ngIf="msgService.messageList?.length > 0 && !isLoading" while isLoading would change depending on the status.
            – ams
            Nov 13 '18 at 1:14











          • @ams Thanks for your answer. as sketchthat correctly said, messageList is a static array. Anyway, I tried something similar to what you said in the comment above, with a isLoading that will control a progress spinner. When user clicks on conversation, isLoading will become true, thus showing only the spinner. Just underneath the part of code where I'm pushing the results to the array, I'm setting isLoading to false to hide the spinner and show the results. Same behavior, would work perfectly sometimes and do issue some others.
            – F. Rusconi
            Nov 13 '18 at 6:07













          0












          0








          0






          You're getting the data from a service, so it could be that you need to use the async pipe.



          <li *ngFor="let message of msgService.messageList | async">
          <p class="message">message.msg</p>
          </li>





          share|improve this answer












          You're getting the data from a service, so it could be that you need to use the async pipe.



          <li *ngFor="let message of msgService.messageList | async">
          <p class="message">message.msg</p>
          </li>






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 13 '18 at 0:24









          amsams

          2187




          2187











          • Message list isn't an observable / promise it is a static array so this won't work.
            – sketchthat
            Nov 13 '18 at 0:47






          • 1




            @sketchthat Ah yes you're right. I always used something like this to prevent this type of error. *ngIf="msgService.messageList?.length > 0 && !isLoading" while isLoading would change depending on the status.
            – ams
            Nov 13 '18 at 1:14











          • @ams Thanks for your answer. as sketchthat correctly said, messageList is a static array. Anyway, I tried something similar to what you said in the comment above, with a isLoading that will control a progress spinner. When user clicks on conversation, isLoading will become true, thus showing only the spinner. Just underneath the part of code where I'm pushing the results to the array, I'm setting isLoading to false to hide the spinner and show the results. Same behavior, would work perfectly sometimes and do issue some others.
            – F. Rusconi
            Nov 13 '18 at 6:07
















          • Message list isn't an observable / promise it is a static array so this won't work.
            – sketchthat
            Nov 13 '18 at 0:47






          • 1




            @sketchthat Ah yes you're right. I always used something like this to prevent this type of error. *ngIf="msgService.messageList?.length > 0 && !isLoading" while isLoading would change depending on the status.
            – ams
            Nov 13 '18 at 1:14











          • @ams Thanks for your answer. as sketchthat correctly said, messageList is a static array. Anyway, I tried something similar to what you said in the comment above, with a isLoading that will control a progress spinner. When user clicks on conversation, isLoading will become true, thus showing only the spinner. Just underneath the part of code where I'm pushing the results to the array, I'm setting isLoading to false to hide the spinner and show the results. Same behavior, would work perfectly sometimes and do issue some others.
            – F. Rusconi
            Nov 13 '18 at 6:07















          Message list isn't an observable / promise it is a static array so this won't work.
          – sketchthat
          Nov 13 '18 at 0:47




          Message list isn't an observable / promise it is a static array so this won't work.
          – sketchthat
          Nov 13 '18 at 0:47




          1




          1




          @sketchthat Ah yes you're right. I always used something like this to prevent this type of error. *ngIf="msgService.messageList?.length > 0 && !isLoading" while isLoading would change depending on the status.
          – ams
          Nov 13 '18 at 1:14





          @sketchthat Ah yes you're right. I always used something like this to prevent this type of error. *ngIf="msgService.messageList?.length > 0 && !isLoading" while isLoading would change depending on the status.
          – ams
          Nov 13 '18 at 1:14













          @ams Thanks for your answer. as sketchthat correctly said, messageList is a static array. Anyway, I tried something similar to what you said in the comment above, with a isLoading that will control a progress spinner. When user clicks on conversation, isLoading will become true, thus showing only the spinner. Just underneath the part of code where I'm pushing the results to the array, I'm setting isLoading to false to hide the spinner and show the results. Same behavior, would work perfectly sometimes and do issue some others.
          – F. Rusconi
          Nov 13 '18 at 6:07




          @ams Thanks for your answer. as sketchthat correctly said, messageList is a static array. Anyway, I tried something similar to what you said in the comment above, with a isLoading that will control a progress spinner. When user clicks on conversation, isLoading will become true, thus showing only the spinner. Just underneath the part of code where I'm pushing the results to the array, I'm setting isLoading to false to hide the spinner and show the results. Same behavior, would work perfectly sometimes and do issue some others.
          – F. Rusconi
          Nov 13 '18 at 6:07

















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f53271482%2fangular-6-ngfor-returns-empty-array-while-array-its-actually-full%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







          這個網誌中的熱門文章

          Barbados

          How to read a connectionString WITH PROVIDER in .NET Core?

          Node.js Script on GitHub Pages or Amazon S3