Angular 2: How to add without loading page

Issue

After clicking a button that calls the add function below, the data is being added into the database but not on the frontend itself. How do you add it without loading the page?

addSection(section: Section) {
    section.name = section.name.trim();
    if(section.name != ''){
        this.onToggleDialogBox();
        this.sectionService
            .registerSection(section)
            .subscribe(
                res => alert(res.message),
                error => console.log(error)
        )
    }else{
        alert('Enter name.');
    }
}

If I use Router, here are the necessary codes:

constructor(
        private router: Router,
        private route: ActivatedRoute,
        private sectionService: SectionService
    ) {}

this.router.navigate(['/information/sections']);

Solution

Try the subscription of rxjs. After executing the method in service, append .finally. In your component, subscribe to that method with the subscription. So that if that method is executed, it will reflect on the component without loading the page

Answered By – user8653311

Answer Checked By – Clifford M. (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.