Directive with controller and parameter in Typescript

0

I am using Typescript 1.8 + angularjs v.1.5.8

I have a directive that I want to call like this:

<div mi-directiva mi-parametro="ctrl.Objeto"> </div>

My directive is declared as follows:

export class miDirectiva implements ng.IDirective{ 
     restrict:'A';
     templateUrl = '/views/template.url';
     controller = 'miController';
     controllerAs = 'ctrl';
     bindToController = true;
     scope:{
              miParametro:'='
           }

     constructor(){}

     static factory(): ng.IDirectiveFactory {
        const directive = () => new miDirectiva();
        return directive;
     }
}

var app=angular.module('App');
app.directive('miDirectiva',[miDirectiva.factory()]);

My controller is declared this way

export class miController{

  miParametro:any;

  constructor(scope:any){ 

     var x = scope.miParametro;//undefined
     var y = this.miParametro; //undefined

     //Como tengo acceso a miParametro?????
  }
}

var app=angular.module('App');
app.controller('miController',['$scope', miController]);

Facts:

  • myParameter is an object
  • According to what I have read, I do not need the $ link or $ compile function, this is not necessary since I am stating that bindToController=true and that my Parameter should already be available in the controller
  • If I receive the object $attrs in the controller if I can get the object by $scope.$eval($attrs.miParametro) but something tells me that this is not done so

How do I access myParameter?

    
asked by Tuco 14.09.2016 в 17:29
source

1 answer

1

I'm already here: link

Movi the scope definition to bindToController and it worked, miParametero already available in miController

export class miDirectiva implements ng.IDirective{ 
     restrict = 'A';
     templateUrl = '/views/template.url';
     controller = 'miController';
     controllerAs = 'ctrl';
     bindToController = {
        miParametro: '='
     }
     scope = {}

     constructor(){}

     static factory(): ng.IDirectiveFactory {
        const directive = () => new miDirectiva();
        return directive;
     }
}

var app=angular.module('App');
app.directive('miDirectiva',[miDirectiva.factory()]);
    
answered by 14.09.2016 / 18:01
source