2014-01-12 108 views
3

我想添加把我的整個類包含控制器在我的指令,出於一些顯而易見的原因範圍和語法是不正確的。 我使用typescript作爲語言和grunt-ts自動生成和編譯。AngularJS + Typescript - 控制器內的指令

/// <reference path="../reference.ts" /> 

directives.directive('myDirective', function():ng.IDirective { 
return { 
    restrict: 'EAC', 
    template: directiveHTML.html, \\ thanks to grunt-ts this work fine 
    controller: MyControllerClass, \\ here I get the error and here I would like to 
             put my own controller class instead of a function 
    link: function (scope, elements, attrs) { 
    } 
} 

});

和這裏的類我的控制器

module Controllers { 
    export class CursorController { 
     constructor($scope, socket){ 
     } 
    } 
} 

其中所有的控制器然後加入到angularJS的控制器模塊的(參考文獻通過咕嚕-TD自動生成的)。

/// <reference path="../reference.ts" /> 
angular.module('controllers',[]).controller(Controllers); 

關於如何解決這個問題的任何線索或建議將是偉大的。

回答

5

你應該能夠做到:

directives.directive('myDirective', function():ng.IDirective { 
return { 
    restrict: 'EAC', 
    template: directiveHTML.html, \\ thanks to grunt-ts this work fine 
    controller: Controllers.CursorController, \\ Lookup controller by name 
    link: function (scope, elements, attrs) { 
    } 
} 
}); 
+0

我以爲我曾試過這個。它像一個魅力。謝謝 :) – giulio

0

我建議是這樣的:

export class MyDirective implements ng.IDirective { 
 

 
    public injection(): Array<any> { 
 
     return [ 
 
      () => { return new MyDirective() } 
 
     ] 
 
    } 
 

 
    public replace: boolean = true; 
 

 
    public controller =() => { 
 
     console.log('trying'); 
 
    } 
 
}

在這裏:

angular.module('myApp', []).directive('myDirective', MyDirective.prototype.injection())