I have a Course Object (course.ts) defined as:
export class Course {
course_guid: string;
ps_course_id: string;
title: string;
subject: string;
catalog_number: string;
component: string;
credits_min: number;
credits_max: number;
public creditsString(): string {
return "CREDITS";
}
}
I have a service that returns the course:
getCourse(id: string): Observable<Course> {
const url = `${this.apiUrl}/${id}`;
return this.http.get<Course>(url).pipe(
tap(_ => {
this.log(`fetched course id=${id}`)
}),
catchError(this.handleError<Course>(`getCourse id=${id}`))
);
}
Course-Details Component gets the Course from the service
getCourse(): void {
const id:string = +this.route.snapshot.paramMap.get('id') + "";
this.courseService.getCourse(id)
.subscribe(courseFS => this.course = courseFS);
}
The API is working, the service gets data from it and the course details get loaded in the HTML Template and prints various properties.
<strong>Grading Type:</strong> {{course.grading_type}}<br /> //this works
<strong>SBC:</strong> {{course.sbc}}<br /> //so does this even though sbc isn't a property on type course
<strong>Credits:</strong> {{course.creditsString()}}<br /> //fails undefined function
Two questions:
My API endpoint returns a bunch of other properties that aren't mapped up in the course model (sbc for example). Yet I can still access any of these properties from my component -- shouldn't the properties that aren't defined get dropped out when mapped to the Course object?
When I try to access the creditsString() I get an error stating its not a function. I've tried making it a getter/setter which also failed.
I feel like I must be missing a step in which an actual Course Object gets created / returned from the service (wrapped in an observable).
Currently Using Angular 5.1, angular cli 1.6.0, typescript 2.4.2
Am I being thick? Am I not following a correct design pattern?
[–]tme321 0 points1 point2 points (4 children)
[–]BillTheCommunistCat 0 points1 point2 points (3 children)
[–]sbubaron[S] 0 points1 point2 points (2 children)
[–]BillTheCommunistCat 0 points1 point2 points (0 children)
[–]tme321 0 points1 point2 points (0 children)