5

I have a question what is the equivalent for directive: compile (pre/post)?

Example in javascript:

angular.module('app').directive('checkBox', function () {
  return {
      //... my directive options

      compile: function () {
           return function () {
               pre: function (scope) {}
               post: function (scope) {}
           }
      }
  }
});

What is TypeScript equivalent for this?

hackp0int
  • 4,052
  • 8
  • 59
  • 95
  • Typescript is just Javascript, so it would be the same. You would probably just want to annotate the scope types with ng.IScope – Martin Oct 06 '14 at 08:20

3 Answers3

8

It will be equivalent to:

public compile = (element: JQuery, attrs: angular.IAttributes, transclude: any): DirectivePrePost => {
            return {
                pre: ($scope: any, element: JQuery, attrs: angular.IAttributes) => {

                },
                post: ($scope: any, element: JQuery, attrs: angular.IAttributes) => {

                }
            };
        }
Ulad Melekh
  • 924
  • 3
  • 17
  • 33
6

if you use the reference from http://definitelytyped.org/tsd/

it is like

compile = (tElem: ng.IAugmentedJQuery, tAttrs: ng.IAttributes, transclude: ng.ITranscludeFunction): ng.IDirectivePrePost => {
            return {
                pre: (scope: IOrderDetailDirScope, iElem: ng.IAugmentedJQuery, iAttrs: ng.IAttributes) => {

                },
                post: (scope: IOrderDetailDirScope, iElem: ng.IAugmentedJQuery, iAttrs: ng.IAttributes) => {

                }
            };
        };
maxisam
  • 21,975
  • 9
  • 75
  • 84
1

You have the return value from compile incorrect. You are supposed to return an object not a function:

compile: function () {
           return { // no `function ()`
               pre: function (scope) {}
               post: function (scope) {}
           }
      }

This code snippet will work as is with TypeScript.

basarat
  • 261,912
  • 58
  • 460
  • 511
  • Wrong what? Your answer is too vague. please correct it. – hackp0int Oct 06 '14 at 08:10
  • 2
    Basarat is saying the return from value from the compile function should be an object, not a function. If you look at his example, it is very clear. You are returning function() {... } you should be returning an object {... } – Martin Oct 06 '14 at 08:56
  • How it concern my question? It erelevant – hackp0int Oct 06 '14 at 11:03