Within my Angular project, I have set up the main component to showcase information about various companies in a table.
Beneath this table lies a button that, when clicked, navigates the user to a page containing input fields to add a new company. Once all necessary details are provided, clicking Submit will result in the addition of the new company to the database.
However, an issue arises: How can we redirect the user back to the main component without disrupting the Single Page Application principle?
public addCompany(newCompanyName:String, newCompanyPassword:String, newCompanyEmail:String){
let newP:any = {
"name": newCompanyName,
"password": newCompanyPassword,
"email": newCompanyEmail,
"coupons": []
}
this.myHttpClient.post<any>("http://localhost:8080/CouponSystemJersey/couponsystem/admin/insertCompany", newP).subscribe(
(res)=>{
console.log("new company");
// How to redirect to adminComponent?
},
(err)=>{console.log(err)}
);
}
I utilize angular routes:
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { LoginComponent } from './login/login.component';
import { AdminComponent } from './admin/admin.component';
import { AddCompanyComponent } from './add-company/add-company.component';
const routes: Routes = [
{path:"login", component: LoginComponent},
{path:"admin", component: AdminComponent},
{path:"addCompany", component: AddCompanyComponent}];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
Desire to transition from addCompany to Admin from within addCompany.
The addCompany function does not execute immediately upon button press; another function (located within addCompany's TS) is triggered first and subsequently calls the addCompany function after validating the fields are not empty.
Error message :
HttpErrorResponse {headers: HttpHeaders, status: 200, statusText: "OK", url: "http://localhost:8080/CouponSystemJersey/couponsystem/admin/insertCompany", ok: false, ...}
error: {error: SyntaxError: Unexpected token A in JSON at position 0 at JSON.parse (<anonymous>) at XMLHtt..., text: "A new customer was inserted"}
headers: HttpHeaders
lazyInit: ƒ ()
lazyUpdate: null
normalizedNames: Map(0) {}
__proto__: Object
message: "Http failure during parsing for http://localhost:8080/CouponSystemJersey/couponsystem/admin/insertCompany"
name: "HttpErrorResponse"
ok: false
status: 200
statusText: "OK"
url: "http://localhost:8080/CouponSystemJersey/couponsystem/admin/insertCompany"
__proto__: HttpResponseBase