5

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[]'.

1 Answer 1

5

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/

Sign up to request clarification or add additional context in comments.

3 Comments

Hi @Hugues but i get Property 'dialogStyle' does not exist on type '{ show: string; }'. while this is what I am trying to implement in Typescript jsbin.com/aDuJIku/2/edit?html,css,js
Can you help me figure out what exactly this issue means " Argument of type 'typeof ModalDialogDirective' is not assignable to parameter of type 'any[]' " I have registered it with the main app as angular .module('modalRun', [ .... ]).directive('modalDialog',ModalDialogDirective)
your second argument in app.directive should be an array maybe? Though it seems to work in js, I would try wrapping the second argument in .directive and .controller in an array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.