K
K
Konstantin2021-02-10 00:13:13
Angular
Konstantin, 2021-02-10 00:13:13

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 groupLayersServicein a component:

@Component({
  selector: "layer-group",
});

class LayerGroup {
    constructor(private groupLayersService: GroupLayersService) {}
}


Next, in the component, I loop through the template and send a specific layer instance to the child component:
<div *ngFor="let layer in groupLayersService.getLayers()">
   <app-block-layer [layer]="layer"></app-block-layer>
</div>


Everything seems to be good. Further, in each component app-block-layerthere is a component app-treethat renders a tree based on the service that receives the layer layer.

The th component app-treeinjects a ready-made tree instance Treebased on subtypes: The

[new ALayers(), new BLayers()]

question is where to create tree instances, apparently in the component 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-treeinjects a ready instance. Moreover, the component does not know exactly - this new ALayersTree(), new BLayersTree()- but it knows that this is the type Treefrom 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 question

Ask a Question

731 491 924 answers to any question