【问题标题】:How to Add Controller In Angular Directive Class In Typescript如何在 Typescript 的 Angular 指令类中添加控制器
【发布时间】:2016-03-18 17:00:10
【问题描述】:

以下是我迄今为止编写的打字稿中的角度指令类:

我的问题是如何为这个指令添加控制器。我不想创建新的控制器类并将该控制器与控制器绑定。我想编写控制器并将 ISOLATE SCOPE 注入到 typescript 中的指令类中

    module Sheet.Directive{
    class InputControl implements ng.IDirective {
    restrict = 'A';
    //require = 'ngModel';
    templateUrl = "../Templates/inputcontrol.html";



    constructor(private $location: ng.ILocationService) {
    }

    link = (scope: ng.IScope, element: ng.IAugmentedJQuery, attrs: ng.IAttributes, ctrl: any) => {
        console.log(this.$location);
    };

    static factory(): ng.IDirectiveFactory {
        const directive = ($location: ng.ILocationService) => new InputControl($location);
        directive.$inject = ['$location'];
        return directive;
    }
}

angular.module("SheetApp").directive("inputControl", InputControl.factory());
}

【问题讨论】:

    标签: javascript angularjs typescript angular-directive angular-controller


    【解决方案1】:

    您实际上可以声明将返回控制器函数的属性controller,例如:

    export class App {
      restrict='E';
      templateUrl='src/app.html';
      scope = {
        a : '@'
      }
      controller = ['$scope',($scope) => {
        console.log("this this controller",$scope.a);
      }];
    }
    

    这是 Plunker 中的示例:http://plnkr.co/edit/j4coAAZ207RHyGsqFPgC(我使用了我的 @directive 技巧来更方便地声明指令。

    【讨论】:

      猜你喜欢
      • 2015-12-01
      • 2017-07-03
      • 1970-01-01
      • 2016-09-01
      • 2017-06-02
      • 2015-12-30
      • 1970-01-01
      • 2023-03-31
      相关资源
      最近更新 更多