Join the Stack Overflow Community
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

Following is my angular directive class in typescript I have written so far:

My question is how I can add controller for this directive. I don't want to create new controller class and bind that controller with controller. I want to write the controller and inject the ISOLATE SCOPE inside the directive class in 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());
}
share|improve this question
up vote 1 down vote accepted

You can actually declare property controller that will return a controller function, like:

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

Here is example in Plunker: http://plnkr.co/edit/j4coAAZ207RHyGsqFPgC (I used my @directive trick for more convinient declaration of directives.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.