I am new to Angular 2 and TypeScript and I'm trying to follow best practices.
Instead of using a simple JavaScript model ({ }), I'm attempting to create a TypeScript class.
However, Angular 2 doesn't seem to like it.
My code is:
import { Component, Input } from "@angular/core"; @Component({ selector: "testWidget", template: "<div>This is a test and {{model.param1}} is my param.</div>" }) export class testWidget { constructor(private model: Model) {} } class Model { param1: string; }
and I'm using it as:
import { testWidget} from "lib/testWidget"; @Component({ selector: "myComponent", template: "<testWidget></testWidget>", directives: [testWidget] })
I'm getting an error from Angular:
EXCEPTION: Can't resolve all parameters for testWidget: (?).
So I thought, Model isn't defined yet... I'll move it to the top!
Except now I get the exception:
ORIGINAL EXCEPTION: No provider for Model!
How do I accomplish this??
Edit: Thanks to all for the answer. It led me to the right path.
In order to inject this into the constructor, I need to add it to the providers on the component.
This appears to work:
import { Component, Input } from "@angular/core"; class Model { param1: string; } @Component({ selector: "testWidget", template: "<div>This is a test and {{model.param1}} is my param.</div>", providers: [Model] }) export class testWidget { constructor(private model: Model) {} }
To create model classes in TypeScript, we can create interfaces. interface IDonutChartModel { dimension: number; innerRadius: number; } const donut: IDonutChartModel = { dimension: 1, innerRadius: 2, }; to create the IDonutChartModel interface.
Angular is a front-end UI framework that holds the information from the browser and sends it to Database. To store the information, Model classes need to be created. Model classes can be of type interface or classes, Let's see how we can create and add attributes manually.
Therefore to generate angular model through angular-CLI, you need to use class schematic with the option –type=model . And there is no special schematic or sub commands available in the name of model. The following command will create recipe model typescript file and it's spec.
I'd try this:
Split your Model into a separate file called model.ts
:
export class Model { param1: string; }
Import it into your component. This will give you the added benefit of being able to use it in other components:
Import { Model } from './model';
Initialize in the component:
export class testWidget { public model: Model; constructor(){ this.model = new Model(); this.model.param1 = "your string value here"; } }
Access it appropriately in the html:
@Component({ selector: "testWidget", template: "<div>This is a test and {{model.param1}} is my param.</div>" })
I want to add to the answer a comment made by @PatMigliaccio because it's important to adapt to the latest tools and technologies:
If you are using
angular-cli
you can callng g class model
and it will generate it for you. model being replaced with whatever naming you desire.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With