【问题标题】:AngularJS+Typescript - Controller inside a directiveAngularJS+Typescript - 指令内的控制器
【发布时间】:2014-01-12 18:35:32
【问题描述】:

我正在尝试将包含控制器的整个类添加到我的指令中,由于某些明显的原因,范围和语法不正确。 我使用 typescript 作为语言,使用 grunt-ts 进行自动生成和编译。

/// <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 的控制器模块中(引用由 grunt-td 自动生成)。

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

任何有关如何解决此问题的线索或建议都会很棒。

【问题讨论】:

    标签: javascript angularjs controller gruntjs typescript


    【解决方案1】:

    你应该可以做到:

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

    【讨论】:

    • 我以为我已经尝试过了。它就像一个魅力。谢谢你:)
    【解决方案2】:

    我建议这样:

    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())

    【讨论】:

      猜你喜欢
      • 2015-09-24
      • 2014-04-07
      • 2013-03-15
      • 1970-01-01
      • 1970-01-01
      • 2015-08-27
      • 2016-04-29
      • 2015-07-29
      • 2014-01-03
      相关资源
      最近更新 更多