0

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

请参阅示例代码中的 ngdoc 或 jsdoc。

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

 * @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: '="
    }
  }
}

4

1 回答 1

0

如果您查看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<string>}

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

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

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

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

于 2019-02-27T21:58:03.457 回答