Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS+Typescript - Controller inside a directive

I am trying to add put my whole class containing the controller inside my directive, put for some obvious reasons scope and syntax is incorrect. I am using typescript as language and grunt-ts for the automatic generation and compiling.

/// <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) {
    }
}

});

and here the class of my controller

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

Where all the controller are then added to the controllers module of angularJS (references are generated automatically by grunt-td).

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

Any clue or suggestion on how to solve this problem would be great.

like image 790
giulio Avatar asked Apr 23 '26 08:04

giulio


1 Answers

You should be able to do :

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) {
    }
}
});
like image 129
basarat Avatar answered Apr 24 '26 21:04

basarat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!