I am making a beginner website and everything was good so far but I have a strange problem now.
When type ng serve --open in the node.js console to start the site on the localhost I receive this message ->
ERROR in src/app/department-detail/department-detail.component.ts(59,32): error TS2339: Property 'employee' does not exist on type 'Department'
My code is that:
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { Location } from '@angular/common';
import { Department } from '../department';
import { DepartmentService } from '../department.service';
import { Employee } from '../employee';
import { EmployeeService } from '../employee.service';
@Component({
selector: 'app-department-detail',
templateUrl: './department-detail.component.html',
styleUrls: ['./department-detail.component.css']
})
export class DepartmentDetailComponent implements OnInit {
department : Department ;
employees: Employee[];
emp: Employee;
average:number=1;
constructor(private route: ActivatedRoute, private departmentService: DepartmentService, private location: Location,private employeeService: EmployeeService) { }
ngOnInit() {
this.getDepartment();
this.getEmployees();
}
getDepartment(): void{
const id = +this.route.snapshot.paramMap.get('id');
this.departmentService.getDepartment(id).subscribe(department => this.department = department)
}
goBack(): void{
this.location.back();
}
save(): void {
this.departmentService.updateDepartment(this.department);
this.goBack();
}
getEmployees(): void {
this.employeeService.getEmployees().
subscribe(employees => this.employees = employees);
}
Select(firstname:string){
firstname = firstname.trim();
if(!firstname){return;}
this.employeeService.getEmployeeByName(firstname).subscribe(Employee => this.emp = Employee);
}
Delete():void{
this.emp = null;
this.average = 1;
}
Show():void{
this.Select(this.department.employee); // this cant be true
this.average = null;
}
}
The problem is in the last
Show function
If I delete employee and save and then add it again the side works but otherwise it does not.
This is my department component.
export class Department{
id: number;
name: string;
location: string;
menId: number;
static Id:number = 1;
constructor(name: string,location: string) {
this.id = Department.Id++;
this.name = name;
this.location = location;
this.menId = 0;
}
}
I am not sure whether I should add a property of type Employee in the Department component.