【问题标题】:What is the equivalent for directive, compile (pre/post) in TypeScript?TypeScript中的指令,编译(前/后)的等价物是什么?
【发布时间】:2014-11-29 21:35:33
【问题描述】:

我有一个问题,指令的等价物是什么:编译(前/后)?

javascript 中的示例:

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

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

什么是 TypeScript 等价物?

【问题讨论】:

  • Typescript 只是 Javascript,所以它是一样的。您可能只想用 ng.IScope 注释范围类型

标签: angularjs angularjs-directive typescript equivalent


【解决方案1】:

compile 的返回值不正确。你应该返回一个对象而不是一个函数:

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

此代码 sn-p 将与 TypeScript 一样工作。

【讨论】:

  • 错了什么?你的回答太模糊了。请改正。
  • Basarat 是说从编译函数返回的值应该是一个对象,而不是一个函数。如果你看他的例子,那就很清楚了。您正在返回 function() {... } 您应该返回一个对象 {... }
  • 它与我的问题有什么关系?相关的
【解决方案2】:

相当于:

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) => {

                }
            };
        }

【讨论】:

    【解决方案3】:

    如果您使用来自http://definitelytyped.org/tsd/的参考

    好像

    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) => {
    
                    }
                };
            };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-29
      • 2020-08-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多