定义指令的参数类型

问题描述 投票:0回答:1

如何在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: '="
    }
  }
}
javascript angularjs jsdoc ngdoc
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>}

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中抛出一个错误,因为该属性需要一个布尔值,但是找到一个字符串。

© www.soinside.com 2019 - 2024. All rights reserved.