| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- import { Component, OnInit } from '@angular/core';
- import {Student} from "./student.model";
- import {StudentService} from "./student.service";
- @Component({
- selector: 'app-students',
- templateUrl: './students.component.html',
- styleUrls: ['./students.component.css']
- })
- export class StudentsComponent implements OnInit {
- studentList?: Student[];
- student?: Student;
- constructor(private studentService: StudentService) { }
- ngOnInit() { this.getStudents(); }
- getStudents(): void {
- this.studentService.getStudents()
- .subscribe(studentList => this.studentList = studentList);
- }
- add(firstname: string, lastname: string, email: string, telephone: string): void {
- firstname = firstname.trim();
- lastname = lastname.trim();
- email = email.trim();
- telephone = telephone.trim();
- this.studentService.addStudent({ firstname, lastname, email, telephone } as Student)
- .subscribe({
- next: (student: Student) => { this.studentList?.push(student); },
- error: () => {},
- complete: () => {
- if (this.studentList != undefined) {
- this.studentService.totalItems.next(this.studentList.length);
- }
- }
- });
- }
- delete(student: Student): void {
- this.studentList = this.studentList?.filter(c => c !== student);
- this.studentService.deleteStudent(student).subscribe(() => {
- // for automatic update of number of students in parent component
- if(this.studentList != undefined) {
- this.studentService.totalItems.next(this.studentList.length);
- }
- }
- );
- }
- deleteAll(): void {
- this.studentService.deleteStudents().subscribe(() => {
- }
- );
- }
- }
|