0

I am using FireStore Realtime Database in Angular.
I want to show a progressbar till the data loads from the database, and hide it after it loads.
This is my code:

  export class KavPatuachChatComponent implements OnInit, AfterViewChecked {

  messagesCollection: AngularFirestoreCollection<MessageItem>;
  messages: Observable<MessageItem[]>;
  mAuthService: AuthService;
  constructor(public afs: AngularFirestore, public authService: AuthService) {
    this.mAuthService = authService;
  }
  ngOnInit() {
    this.getChatData();
}
  getChatData() {
    this.messagesCollection = this.afs.collection<MessageItem>('chat_messages', ref => ref.orderBy('DateTime'));
    this.messages = this.messagesCollection.valueChanges();
  }

How do I know when the data loads, in order to hide the progress bar then?

2 Answers 2

1
this.afs.collection<MessageItem>('chat_messages', ref => {ref.orderBy('DateTime'); 
//code to hide progress bar
});
Sign up to request clarification or add additional context in comments.

5 Comments

Yes, but How do I know when the data loads? Where is there an event or something like that to know that?
the above line that I mentioned is a call back function. It will be called once the data is loaded. alternatively, you can use subscribe() or other call back functions
Oh, but it gives me an error: Argument of type '(ref: CollectionReference) => void' is not assignable to parameter of type 'QueryFn'. Type 'void' is not assignable to type 'Query'
try this this.messagesCollection.valueChanges().map(actions => { //code goes here });
Could you please expand on your answer to prevent explaining the solution in the comments. thanks
1

can't you use a *ngIf in the HTML component ? Use a boolean variable to control this before and after the load like this

in .ts

this.dataServiceProcessed = true \ false

In HTML

<div *ngIf="dataServiceProcessed">  
  Display something
</div>

2 Comments

Yes, but How do I know when the data loads? Where is there an event or something like that to know that?
I'm not sure if there's a more efficient way, You could create a data service and then subscribe like here stackoverflow.com/questions/47338271/…, so after this.dataA= dataAJSON then set your boolean variable that controls the *ngIf

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.