javascriptangularjscoffeescriptangularjs-provider

angular provider error - must define $get factory method


This coffee script code is trying to create an angular provider but I get this message: Provider 'ItemsProvider' must define $get factory method.

I have the $get method set. Any idea of what is happening?

'use strict'

    app = angular.module('logica-erp') 

    app.provider 'ItemsProvider', [ ->

        this.$get = ->
                return {

                }

    ]

It fail to load with this message:

Error: [$injector:modulerr] Failed to instantiate module logica-erp due to:
[$injector:pget] Provider 'ItemsProvider' must define $get factory method.

EDIT: This is the javascript generated:

(function() {
  'use strict';
  var app;

  app = angular.module('logica-erp');

  app.provider('ItemsProvider', [
    function() {
      return this.$get = function() {
        return {};
      };
    }
  ]);

}).call(this);

Solution

  • CoffeeScript introduces syntax sugar coating that may be poorly understood by both readers and adepts. It is always a good idea to compile it to JS to see what's going on. Implicit returns appear to be the biggest troublemakers in my practice.

    In this case CS code is compiled to this

    app.provider('ItemsProvider', [
      function() {
        return this.$get = function() {
          return {};
        };
      }
    ]);
    

    Here provider constructor function returns a value of this.$get (a function) and not this object. Constructor function shouldn't return anything (except the rare case when it should):

    app.provider('ItemsProvider', [
      function() {
        this.$get = function() {
          return {};
        };
      }
    ]);
    

    Beware the arrows.