This is a note-taking application created using Angular and Firebase. The primary functionalities include adding items and displaying them. I noticed a strange behavior where my ngOnInit()
method is being called twice every time I add an item. As shown in the screenshot, the console.log(this.notepads)
statement is triggered twice upon adding an item.
Do you think there is an issue causing the ngOnInit()
method to be called twice, or is it expected behavior?
import { Component, OnInit } from '@angular/core';
import { NotepadService } from 'src/app/services/notepad.service';
import { Notepad } from 'src/app/model/note';
@Component({
selector: 'app-add-note',
templateUrl: './add-note.component.html',
styleUrls: ['./add-note.component.css']
})
export class AddNoteComponent implements OnInit {
title:boolean=false;
noteTitle:string;
noteDescription:string;
constructor(private notepadService:NotepadService) { }
ngOnInit(): void {
}
onSubmit() {
if(!this.title || !this.noteTitle) {
alert('Task is empty')
} else {
let note:Notepad = {
title:this.noteTitle,
description:this.noteDescription
}
this.noteTitle = ''
this.noteDescription = ''
this.title = false
this.notepadService.addNote(note)
}
}
}
import { Component, OnInit} from '@angular/core';
import { NotepadService } from 'src/app/services/notepad.service';
import { Notepad } from 'src/app/model/note';
import { NgForm } from '@angular/forms';
@Component({
selector: 'app-notepad',
templateUrl: './notepad.component.html',
styleUrls: ['./notepad.component.css']
})
export class NotepadComponent implements OnInit {
notepads:Notepad[]=[];
modal:boolean=false;
selectedNote:Notepad= {
id:'',
title: '',
description: ''
};
constructor(private notepadService:NotepadService) {
}
ngOnInit(): void {
this.notepadService.getNotepads().subscribe(notepad=> {
this.notepads=notepad
console.log(this.notepads)
})
}
onSelect(note:Notepad){
this.modal = !this.modal
this.selectedNote = note;
}
onSubmit(reg:NgForm) {
const note = reg.value;
note.id = this.selectedNote.id
this.notepadService.updateNote(note)
this.modal = false
}
removeNote() {
if(confirm('Are you sure?')) {
this.notepadService.deleteNote(this.selectedNote)
this.modal = false
}
this.modal = false
}
}