最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - AngularJS+Typescript - Controller inside a directive - Stack Overflow

matteradmin5PV0评论

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 piling.

/// <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.

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 piling.

/// <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.

Share Improve this question edited Jan 12, 2014 at 18:59 giulio asked Jan 12, 2014 at 18:35 giuliogiulio 6,1553 gold badges24 silver badges20 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

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

I'd suggest something like this:

export class MyDirective implements ng.IDirective {

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

    public replace: boolean = true;

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

And here:

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

Post a comment

comment list (0)

  1. No comments so far