2015-08-18 73 views
2

我想通過使用靜態數據來引用常量的名稱,但由於執行順序,它不會工作。這應該怎麼做。在TypeScript中爲AngularJS常量使用靜態字符串

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

的$注射不起作用,當我使用TaskConfiguration.named代替「taskConfig」,因爲它沒有初始化,在翻譯時到達線。但我能做些什麼呢?

回答

1

,但它不工作了,因爲執行順序

一般來說,這是你碰到與out風險的。

你的情況,你可以重新排序類:

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

甚至更​​好移動到外部模塊

實際上,這些是放置在不同角度模塊中的兩個文件。也許有另一種方法來注入依賴關係?

確實使用外部模塊並避開--out。更多:https://github.com/TypeStrong/atom-typescript/blob/master/docs/out.md

+0

實際上,這些是放置在不同角度模塊中的兩個文件。也許有另一種方法來注入依賴關係? – dec

+0

@dec yup這就是爲什麼我說使用外部模塊。我有更多的文檔供您參考 – basarat

+0

好的。我知道你建議我使用另一個構建工具來放在一起,而不是TypeScript的--out選項。對? – dec

相關問題