How to document a factory that returns a class in angular with ngdoc?

[亡魂溺海] 提交于 2019-12-05 03:01:35

This is how I would do it not having any experience with Angular in particular:

/**
 * @ngdoc service
 * @name fooApp.User
 * @description User factory.
 */

angular.module('fooApp').factory('User', function(){

    /**
     * @ngdoc method
     * @constructs fooApp.User
     * @description Initializes a new User object with a name
     */
    function User(name){
        this.name = name;
    }

    User.prototype.greet = function(){
        return "Howdy, " + this.name;
    }

    return User;
});

This is opinionated answer.
I had trouble with Angular documentation, and I even wrote a blog about it, Sigh, AngularJS Documentation

My conclusion was to build a new one. Angular-jsdoc is the outcome.

Here is excerpts from github.

Angular-JSDoc

JSDoc 3 Template for AngularJS.
A JSDoc plugin and template for AngularJS, nothing else!

Features

  • Right side TOC, table of contents, for navigation by Directives, Services, Controllers, etc
  • Read and process @ngdoc tag

How Does It Look Like

I created a fork/pull request to add @constructor support to gulp-ngdocs as available in grunt-ngdocs: https://github.com/nikhilmodak/gulp-ngdocs/pull/91. All this does is change the basic usage section to include the new keyword.

Then use in a fashion similar to, but slightly different from, @SGD's answer. Make sure that @ngdoc directive on the service specifies function.

Example:

/**
 * @ngdoc function
 * @name yourModule.yourService
 * @description
 *
 * Your short description
 *
 * @constructor
 * @param {string} yourConstructorParameter A string paramter
 * @returns {Object} An object instance of type YourClassName
 * 
 */       

angular
  .module('yourModule', [])
  .factory('YourClassName', function() {

    /**
     * @ngdoc object
     * @name yourModule.type:YourClassname
     * @description
     *
     * Short description.
     * 
     */

    function YourClassName(yourConstructorParameter) {
      this.yourConstructorParameter = yourConstructorParameter;
      this.yourMethod = function() {};
    }


  /**
   * @ngdoc function
   * @name yourModule.type:YourClassName#yourPrototypeMethod
   * @methodOf yourModule.type:YourClassName
   * @description
   *
   * Short Description.
   *    
   * @returns {Object} The object passed on instantiation.
   */

    YourClassName.prototype.yourPrototypeMethod = function() {
      return this.yourConstructorParameter;
    };

    return YourClassName;

  });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!