Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue angular directives using Typescript

Hi I am trying to implement following angular directive using Typescript classes,

angular.module('mota.main', []).directive('modalDialog', function() {
 return {
restrict: 'E',
scope: {
  show: '='
},
replace: true, // Replace with the template below
transclude: true, // we want to insert custom content inside the directive
link: function(scope, element, attrs) {
  scope.dialogStyle = {};
  if (attrs.width)
    scope.dialogStyle.width = attrs.width;
  if (attrs.height)
    scope.dialogStyle.height = attrs.height;
  scope.hideModal = function() {
    scope.show = false;
  };
},
templateUrl = 'src/app/selection.ts'
};
});

This is the template:

   <div class='ng-modal' ng-show='show'>
        <div class='ng-modal-overlay' ng-click='hideModal()'></div>
        <div class='ng-modal-dialog' ng-style='dialogStyle'>
           <div class='ng-modal-close' ng-click='hideModal()'>X</div>
           <div class='ng-    modal-dialog-content' ng-transclude></div>
        </div>
    </div>

This is my approach ,

export class ModalDialogDirective implements ng.IDirective {
    public restrict = "E";
    public scope = {show: '='};
    public require = ['ngModel'];
    public transclude = true;
    public templateUrl = 'src/app/selection.ts';
    public replace = true;
    public link(scope: ng.IScope, element: JQuery, attrs: ng.IAttributes)
        {
            scope.dialogStyle = {};
            if (attrs.width){
              scope.dialogStyle.width = attrs.width;
            }
            if (attrs.height){
              scope.dialogStyle.height = attrs.height;
            }
            scope.hideModal = function() {
              scope.show = false;
            };
        }
}

This his how it gets called in html :

<modal-dialog show='modalShown' width='400px' height='60%'>
  <p>Modal Content Goes here<p>
</modal-dialog>

I am getting issues as: Property 'dialogStyle' does not exist on type '{ show: string; }'.

Property 'width' does not exist on type 'IAttributes'.

Argument of type 'typeof ModalDialogDirective' is not assignable to parameter of type 'any[]'.

like image 238
shailbenq Avatar asked Mar 23 '15 20:03

shailbenq


1 Answers

Your link function should accept a scope of an extended type. Declare an interface that extends ng.IScope to supply your parameters, then use that interface in your link function:

interface ImyScope extends ng.IScope{
    dialogStyle:any;
    hideModal:()=>void;
    show:boolean;
 }

public link(scope: ImyScope, element: JQuery, attrs: ng.IAttributes)
    {
        scope.dialogStyle:any = {};
        if (attrs.width){
          scope.dialogStyle.width = attrs.width;
        }
        if (attrs.height){
          scope.dialogStyle.height = attrs.height;
        }
        scope.hideModal = function() {
          scope.show = false;
        };
    }

If you wish to get some templates to get started with angular and typescript, I suggest that you check SideWaffle: http://sidewaffle.com/

like image 159
Hugues Stefanski Avatar answered Sep 30 '22 10:09

Hugues Stefanski