Haseeb Khan
Haseeb Khan

Reputation: 1040

Angular Typescript Controller Constructor not called

I am using Angular with type script.

I have model class in type script

module app.form {

    export class Patient{

        constructor(
            public firstName: string,
            public lastName: string,
            public gender: string,
            public birthDate: Date,
            public currentMedications: string,
            public notes: string,
            public isMedicare: boolean,
            public medicareName: string,
            public medications: string[],
            public ethnicity: string[],
            public billingInfo: BillingInformation,
            public specimenInfo: SpecimenInformation,
            public assayRequested: AssayRequested,
            public authorizationDetail: AuthorizationDetail
        ) {

        }
    }
}

My Controller code using type script is like this

module app.form {
    export class MainController {
        constructor(public patient: Patient) {

        } 
    }   
}

My app file look like this.

module app.form {
    angular
        .module("formApp", [
            "ngMaterial",
            "ngMdIcons",
            "ngMessages"
        ])
        .controller("MainController", MainController);
}

I am registering all the script in html file in this order

<script src="Scripts/angular.min.js"></script>
    <script src="Scripts/angular-resource.min.js"></script>
    <script src="Scripts/angular-animate/angular-animate.min.js"></script>
    <script src="Scripts/angular-aria/angular-aria.min.js"></script>
    <script src="Scripts/angular-messages.min.js"></script>
    <script src="Scripts/angular-material/angular-material.min.js"></script>
    <script src="Scripts/angular-material-icons/angular-material-icons.min.js"></script>

    <script src="app/model.js"></script>
    <script src="app/controllers/main-controller.js"></script>
    <script src="app/app.js"></script>

When I run my application I see this error

enter image description here

Where I am doing wrong?

Upvotes: 4

Views: 2332

Answers (2)

Igor
Igor

Reputation: 62228

Typically you do not inject data but retrieve it from a service. By data I mean Patient in your main controller. As mentioned by @toskv in the comments your constructor is not parameterless which means that angular is going to expect that you can inject parameters into it based o the name of the parameter OR name in the injection args string array. You have nothing that matches variable name patient to be injected.

Change your code like this. If you want to retrieve a specific patient create a Service or Factory to do so and inject it into your controller and register that Service/Factory with angular.

module app.form {
    export class MainController {
        public patient: Patient; // make it a field
        constructor() {
           this.patient = new Patient(/*parameters here if any*/); //new instance
        } 
    }   
}

Upvotes: 0

Haseeb Khan
Haseeb Khan

Reputation: 1040

To resolve the error one must use the static $inject in the controller while using typescript

static $inject = [];

Upvotes: 1

Related Questions