Nodejs 4567 - Merged-27-36
Nodejs 4567 - Merged-27-36
Practical 9
axios.defaults.baseURL = environment.apiUrl
“ login.component.ts “
1
FACULTY OF ENGINEERING AND
TECHNOLOGY
Subject Name: MSWD LAB
Subject Code: 203105380
B.Tech. 3rd year 6TH semester
ngOnInit(): void {
if(localStorage.getItem('token') != "" && localStorage.getItem('token') != null){
this.router.navigateByUrl('/dashboard')
}
} registerAction() { this.isSubmitting
= true; let payload =
{ name:this.name,
email:this.email, password:this.password,
confirmPassword:this.confirmPassword
2
FACULTY OF ENGINEERING AND
TECHNOLOGY
Subject Name: MSWD LAB
Subject Code: 203105380
B.Tech. 3rd year 6TH semester
}
this.userAuthService.register(payload)
.then(({data}) => { localStorage.setItem('token',
data.token)
this.router.navigateByUrl('/dashboard') return data }).catch(error => {
this.isSubmitting = false; if (error.response.data.errors != undefined) {
this.validationErrors =
error.response.data.errors
} return
error
})
}
}
“ app.module.ts “
“ app-routing-module.ts “
3
FACULTY OF ENGINEERING AND
TECHNOLOGY
Subject Name: MSWD LAB
Subject Code: 203105380
B.Tech. 3rd year 6TH semester
4
FACULTY OF ENGINEERING AND
TECHNOLOGY
Subject Name: MSWD LAB
Subject Code: 203105380
B.Tech. 3rd year 6TH semester
Practical 10
Aim: - Demonstrating a CRUD Api With the help of Angular, Node and
MongoDB.
“ Create.comp.ts ”
@angular/forms';
@Component({ selector: 'app-create',
templateUrl: './create.component.html', styleUrls:
['./create.component.css']
}) export class CreateComponent { form!:FormGroup;
//Contructor constructor( public
studentService: StudentService, private router: Router
) { } ngOnInit(): void { this.form = new FormGroup({ id: new
FormControl('', [Validators.required]), title: new FormControl('',
[Validators.required]), body: new FormControl('', Validators.required) });
} get
frmctl(){
return this.form.controls;
} submit(){ console.log(this.form.value);
this.studentService.create(this.form.value).subscribe((r es:any) =>
{ console.log('Student created successfully!');
this.router.navigateByUrl('student/index');
})
}
}
“ Index.comp.ts ”
5
FACULTY OF ENGINEERING AND
TECHNOLOGY
Subject Name: MSWD LAB
Subject Code: 203105380
B.Tech. 3rd year 6TH semester
}
deleteStudent(id:number){ this.studentService.delete(id).subscribe(res
=> { this.students = this.students.filter(item => item.id !== id);
console.log('Student deleted successfully!'); })
}
}
“ Update.comp.ts ”
6
FACULTY OF ENGINEERING AND
TECHNOLOGY
Subject Name: MSWD LAB
Subject Code: 203105380
B.Tech. 3rd year 6TH semester
} submit(){ console.log(this.form.value);
this.studentService.update(this.id, this.form.value).subscribe((res:any) =>
{ console.log('Student updated successfully!');
this.router.navigateByUrl('student/index');
})
}}
“ Student.Routing.module.ts ”
7
FACULTY OF ENGINEERING AND
TECHNOLOGY
Subject Name: MSWD LAB
Subject Code: 203105380
B.Tech. 3rd year 6TH semester
“ Main.ts ”
import { platformBrowserDynamic } from '@angular/platformbrowser-
dynamic';
import { AppModule } from './app/app.module';
platformBrowserDynamic().bootstrapModule(AppModule)
.catch(err => console.error(err));
-:OutPut:-
8
FACULTY OF ENGINEERING AND
TECHNOLOGY
Subject Name: MSWD LAB
Subject Code: 203105380
B.Tech. 3rd year 6TH semester