Although this question may be older, it's important to clarify that the model variable has been added incorrectly to your test widget class. If you require a Model variable, avoid passing it through the component constructor. The intended method is to pass services or other injectables in that manner. When instantiating your test widget within another component and needing to pass a model object, consider utilizing the Angular core OnInit and Input/Output design patterns.
For example, your code should resemble something similar to this:
import { Component, Input, OnInit } from "@angular/core";
import { YourModelLoadingService } from "../yourModuleRootFolderPath/index"
class Model {
param1: string;
}
@Component({
selector: "testWidget",
template: "<div>This is a test and {{model.param1}} is my param.</div>",
providers: [ YourModelLoadingService ]
})
export class testWidget implements OnInit {
@Input() model: Model; //Use this if you want the parent component instantiating this
//one to be able to directly set the model's value
private _model: Model; //Use this if you only want the model to be private within
//the component along with a service to load the model's value
constructor(
private _yourModelLoadingService: YourModelLoadingService //This service should
//usually be provided at the module level, not the component level
) {}
ngOnInit() {
this.load();
}
private load() {
//Include code for making your component read-only,
//possibly incorporating a busy spinner on top of your view
//This helps prevent bugs and informs the user of ongoing processes
//If using the Input model approach so the parent scope can set the contents of the model,
//add an event callback for when the model is updated by the parent
//On event: once loading is completed, disable read-only mode and any accompanying spinner
//If relying on a service to populate the model, incorporate code calling your
//service functions retrieving the model's value
//Upon setting the model's value, disable read-only mode and any spinner present. Depending on whether utilizing Observables or other methods,
//this step may involve additional callbacks as well
}
}
A struct/model-like class should not be injected, as it would allow only a single shared instance within its provided scope. In this scenario, a new Model instance is created each time testWidget is instantiated by the dependency injector. Providing it at the module level ensures a single shared instance for all components and services within the module.
Following standard Object-Oriented principles, create a private model variable within the class. Any information needed during instantiation should be handled by a service (injectable) provided by the parent module. This aligns with Angular's intended approach to dependency injection and communication.
Additionally, as mentioned by others, declare your model classes in a separate file and import the class accordingly.
I highly recommend revisiting the angular documentation guide to review fundamental concepts related to annotations and class types:
https://angular.io/guide/architecture
Pay close attention to sections covering Modules, Components, and Services/Dependency Injection, crucial for grasping Angular's architectural usage. Given Angular's emphasis on architecture, understanding application structure is vital. While Angular automates separation of concerns, dependency injection factories, and cross-browser compatibility, correct application architecture remains essential for expected functionality.