Answer the question
In order to leave comments, you need to log in
How to extend a class (add a new attribute to the class object) programmatically?
The following situation:
Assume there are the following classes (TypeScript pseudocode):
class Project {
id: number;
name: string;
stages: Stage[];
constructor() {
this.stages = []
}
}
class Stage {
id: number;
name: string;
jobs: Job[];
constructor() {
this.jobs= []
}
}
class Job {
id: number;
name: string;
}
@Component
export class ProjectFormComponent implements OnInit {
constructor (private projectService: ProjectService) { }
projects: Projects[];
onInit() {
this.projectService.getPorjects()
.subscribe(projects => this.projects = projects);
}
OnEditProjectButtonClick(project) {
this.projectService.getStageByProjectID(project)
.subscribe(stages => {
project.stages = stages (как то нужно добавить атрибут checked к объекту stages)
project.stages.filter(stage => stage.checked) (но у нас нет свойства checked)
}
}
}
<ul>
<li *ngFor="let stage of project.stages">
<input type="checkbox" [checked] = "stage.checked" (вот здесь необходимо свойство checked, а его нет в классе)
{{stage.name}}
</li>
</ul>
Answer the question
In order to leave comments, you need to log in
can be used.
Or
Finally, you can define the IChecked interface and type it:stage['checked']
stage: Stage & IChecked
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question