【问题标题】:Defining type of parameter for the directive为指令定义参数类型
【发布时间】:2019-07-21 04:45:24
【问题描述】:

angularjs中如何定义指令的参数类型? & 绑定使用什么类型?

参见示例代码中的 ngdoc 或 jsdoc。

更新: 我的目标是获得以下问题的答案

 * @param {<< what to write here? >>} parentContextExpression
 * @param {<< what to write here? >>} oneWayParameter

angular.module('app', [])
  .directive('exampleDir', exampleDir);

/**
 * @ngdoc directive
 * @module app
 * @name app.directive:exampleDir
 * @param {<< what to write here? >>} parentContextExpression
 * @param {<< what to write here? >>} oneWayParameter
 * @param {Object=} twoWayParameter
 * @usage
 * <example-dir
 *   parent-context-expression="externalFn()"
 *   one-way-parameter="parentScopeVariable"
 *   two-way-parameter="parentScopeObject"
 * ></example-dir>
 **/
function exampleDir() {
  return {
    template: '...',
    scope: {
      parentContextExpression: '&',
      oneWayParameter: '@',
      twoWayParameter: '="
    }
  }
}

【问题讨论】:

标签: javascript angularjs jsdoc ngdoc


【解决方案1】:

如果您查看Angular Material 代码,您将看到此答案的来源。

这是一个简化版本,看起来更像问题中的来源。

/**
 * @ngdoc directive
 * @name exampleDir
 *
 * @param {string} one-way-parameter A One way.
 * @param {expression=} parent-context-expression An Expression
 */
function ExampleDir() {
  return {
    restrict: 'E',

    scope: {
      oneWayParameter: '@?oneWayParameter',
      parentContextExpression: '=?parentContextExpression'
    },
}

根据我使用 Closure Compiler 的经验(它与 JSDoc 不同,也不是 ngDoc):

scope 的类型是{Object&lt;string&gt;}

scope 参数的值在 $onInit 在控制器的 class 上运行之前不存在,因此它们必须在构造函数中可以为空。您可以像这样提供这些类型;

class SomeCtrl {
  constructor() {
    /** @type {?boolean} */
    this.oneWayParameter;
  }

  $onInit() {
    this.oneWayParameter = 'this is not a boolean';
  }
}

在此示例中,this.oneWayParameter = 'this is not a boolean'; 在 Closure 中引发错误,因为该属性需要一个布尔值,但找到了一个字符串。

【讨论】:

    猜你喜欢
    • 2015-11-16
    • 2016-08-04
    • 2021-05-02
    • 2018-01-28
    • 2021-09-11
    • 1970-01-01
    • 1970-01-01
    • 2020-08-02
    • 1970-01-01
    相关资源
    最近更新 更多