title: AngularJS : Why is my factory always undefined in my controller?
body: <p>My factory is undefined in my controller and I cannot figure out why. I have created a simple example to illustrate.</p>
  
  <p><strong>Here I create the app:</strong></p>
  
  <pre><code>var ruleApp = angular
      .module( "ruleApp", [ 
          "ngRoute",  
          "ruleApp.NewFactory1",
          "ruleApp.NewFactory2",
      ] );
  </code></pre>
  
  <p>In this dummy example I'd like to build a factory that does something simple, show an alert box. I'll show two methods of doing this (one works, one does not).</p>
  
  <p><strong>Factory 1:</strong></p>
  
  <pre><code>angular
      .module('ruleApp.NewFactory1', [])
      .factory('NewFactory1', function() {
          return function(msg) {
              alert(msg);
          };
    });
  </code></pre>
  
  <p><strong>Factory 2:</strong></p>
  
  <pre><code>angular
      .module('ruleApp.NewFactory2', [])
      .factory('NewFactory2', function() {
          var showMessageFunction = function(msg) {
              alert(msg);
          };
          return 
          {
              showMessage: showMessageFunction
          };
    });
  </code></pre>
  
  <p>Notice the return type of factory 1 is a function and the return type of factory 2 is an object with a property (which is of type function). </p>
  
  <p><strong>Now look at how I'd like to use both of these factories in my controller:</strong></p>
  
  <pre><code> ruleApp.controller( "RuleController", function( $scope, NewFactory1, NewFactory2 ) {
      NewFactory1("1");
      NewFactory2.showMessage("2");
  });
  </code></pre>
  
  <p>This is where the problem gets exposed. During execution, I am prompted with the alert box for <code>NewFactory1("1");</code>, but it fails during execution of <code>NewFactory2.showMessage("2");</code> because NewFactory2 is undefined (TypeError: Cannot call method 'showMessage' of undefined).</p>
  
  <p>Can you help me spot the issue? I want to be able to use factories like NewFactory2 because I want the factories to be able to do more than just one thing (i.e. have more than one single function). By the way, I'm using Angular version 1.2.1.</p>