2

I want to reference the names for a constant by using static data, but it does not work out, because of the execution order. How should this be done.

module tasks {

    export class TasksController {

        //name of the controller used for referencing or defining this controller
        public static named: string = 'tasksController';
        public static templateUrl: string = 'apps/demo/tasks/tasks.tpl.html';

        //dependencies to other services etc., referenced in the constructor
        static $inject: string[] = ['tasksConfig'/*TasksConfiguration.named*/];

        //constructor getting injected dependency instances
        constructor(appConfig: ApplicationConfiguration) {
        }

    }


    angular
        .module('Tasks', [])
        .controller(TasksController.named, TasksController);
}


module tasks {

    export class TasksConfiguration {

        //id of the configuration used for referencing or defining
        public static named = 'tasksConfig';

        public static values: ApplicationConfiguration = {
            appId: 'demo.tasks',
            appName: 'App_Name_Tasks',
            appCategory: 5,
            rootUrl: '/tasks'
        };
    }

    angular.module('Demo')
        .constant(TasksConfiguration.named, TasksConfiguration.values);
}

The $inject does not work, when I use TaskConfiguration.named instead of 'taskConfig' because it is not initialised, when the interpreter reaches the line. But what can I do instead?

1 Answer 1

1

but it does not work out, because of the execution order

In general this is a risk you run into with out.

In your case you can just reorder the classes :

module tasks {

    export class TasksConfiguration {

        //id of the configuration used for referencing or defining
        public static named = 'tasksConfig';

        public static values: ApplicationConfiguration = {
            appId: 'demo.tasks',
            appName: 'App_Name_Tasks',
            appCategory: 5,
            rootUrl: '/tasks'
        };
    }

    angular.module('Demo')
        .constant(TasksConfiguration.named, TasksConfiguration.values);
}

module tasks {

    export class TasksController {

        //name of the controller used for referencing or defining this controller
        public static named: string = 'tasksController';
        public static templateUrl: string = 'apps/demo/tasks/tasks.tpl.html';

        //dependencies to other services etc., referenced in the constructor
        static $inject: string[] = ['tasksConfig'/*TasksConfiguration.named*/];

        //constructor getting injected dependency instances
        constructor(appConfig: ApplicationConfiguration) {
        }

    }


    angular
        .module('Tasks', [])
        .controller(TasksController.named, TasksController);
}

Or even better move to external modules.

In reality these are two files placed in different angular modules. Maybe there is another way to inject dependencies?

Indeed Use external modules and steer clear of --out. More : https://github.com/TypeStrong/atom-typescript/blob/master/docs/out.md

Sign up to request clarification or add additional context in comments.

5 Comments

In reality these are two files placed in different angular modules. Maybe there is another way to inject dependencies?
@dec yup that's why I said use external modules. I've more docs for your perusal
Okay. I understand you suggest me to use another build tool to put all together, and not the --out option of TypeScript. Right?
I am not using out. I got several js files but the ordering of including and executing these is undefined, I think
but the ordering of including and executing these is undefined < How are you loading these files ... that is the tool ordering these for you at the moment

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.