4

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?

1
  • 2
    patient isn't registered as anything, so angular doesn't know what to inject when you ask for it in the MainController. Commented Mar 13, 2016 at 18:05

2 Answers 2

1

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

static $inject = [];
Sign up to request clarification or add additional context in comments.

1 Comment

That is 1 option. You can also do controller("MainController", ["inject1", "inject2", MainController]); In your case though you did not provide any injection (parameters or service for that matter) for your constructor which is why it failed with that error.
0

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
        } 
    }   
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.