I'm currently working on a user registration application in Angular. My goal is to notify the user upon successful account creation or if an error occurs. I've been attempting to use a snackbar for this purpose, but it's not working as expected. Although I added an alert to check if I'm entering the function, the snackbar notification remains elusive. Any assistance would be greatly appreciated.
Here is the TypeScript code I'm using:
export class AdduserComponent implements OnInit {
constructor(private formBuilder:FormBuilder,private userService:UsersService,private router:Router,private snackBar: MatSnackBar) { }
addForm: FormGroup;
selected = 'option2';
passwordsMatcher = new RepeatPasswordEStateMatcher;
ngOnInit()
{
this.addForm = this.formBuilder.group({
id: [],
userName: ['', Validators.required],
password:new FormControl( '',[ Validators.required]),
passwordAgain: new FormControl('',[ Validators.required]),
userRole:['',Validators.required],
},{ validator: RepeatPasswordValidator });
}
onSubmit() {
if (this.addForm.valid)
{
this.userService.createUser(this.addForm.value)
.subscribe( data => {
console.log(data);
alert("User created");
//this.router.navigate(['adduser']);
},error=>
{console.log(error)
alert("Username already exists, please choose a different one.");
this.snackBar.open("message", "action", {
duration: 2000,
});
this.addForm.controls.userName.reset();
});
}