Answer the question
In order to leave comments, you need to log in
Where is the best place to create class instances in Angular?
I have an array of objects of type Layer
:
let layers = [new ALayers(), new BLayers()];
I get them from a service groupLayersService
in a component:
@Component({
selector: "layer-group",
});
class LayerGroup {
constructor(private groupLayersService: GroupLayersService) {}
}
<div *ngFor="let layer in groupLayersService.getLayers()">
<app-block-layer [layer]="layer"></app-block-layer>
</div>
app-block-layer
there is a component app-tree
that renders a tree based on the service that receives the layer layer
. app-tree
injects a ready-made tree instance Tree
based on subtypes: The [new ALayers(), new BLayers()]
app-block-layer
- it is in it that I specifically know what type new ALayers(), new BLayers()
I am working with. So I can know what type of tree to create new ALayersTree(), new BLayersTree()
so that the component app-tree
injects a ready instance. Moreover, the component does not know exactly - this new ALayersTree(), new BLayersTree()
- but it knows that this is the type Tree
from which the new ALayersTree(), new BLayersTree()
.
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question