AngularJs & Cannot read property of 'SetNavBanner' of undefined
<p>I'm working on a simple single page application using angular. I have one module and several different controllers. Within my module I have a factory service that sets on css on the page. The factory service is called within each of the different controllers. When the factory service is called from a controller, I get the following error:
"Cannot read property 'SetNavBanner' of undefined.</p>

<p>My code is posted below,  Please help. Thank you in advance.</p>

<p>index.html</p>

<pre><code>&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
  &lt;head&gt;
    &lt;meta charset="utf-8"&gt;
    &lt;meta http-equiv="X-UA-Compatible" content="IE=edge"&gt;
    &lt;meta name="viewport" content="width=device-width, initial-scale=1"&gt;
    &lt;title&gt;Bootstrap 1 Template&lt;/title&gt;
    &lt;!-- Bootstrap --&gt;
    &lt;link href="css/bootstrap.min.css" rel="stylesheet"&gt;
    &lt;link rel="stylesheet" href="css/font-awesome.min.css" /&gt;
    &lt;link rel="stylesheet" href="css/font-awesome.css" /&gt;
    &lt;link rel="stylesheet" href="js/fancybox/jquery.fancybox.css" type="text/css" media="screen" /&gt;
    &lt;link rel="stylesheet" type="text/css" href="css/isotope.css" media="screen" /&gt;
    &lt;link rel="stylesheet" href="css/style.css"&gt;
    &lt;!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries --&gt;
    &lt;!-- WARNING: Respond.js doesn't work if you view the page via file:// --&gt;
    &lt;!--[if lt IE 9]&gt;
      &lt;script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"&gt;&lt;/script&gt;
      &lt;script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"&gt;&lt;/script&gt;
    &lt;![endif]--&gt;
    &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.0/angular.min.js"&gt;&lt;/script&gt;
  &lt;/head&gt;
  &lt;body ng-app="appMod"&gt;

    &lt;!-- Start: Header --&gt;
    &lt;div ng-include="'templates/header.html'"&gt;&lt;/div&gt;
    &lt;!-- End: Header --&gt;
    &lt;!-- Start: Content --&gt;
    &lt;div ng-view&gt;&lt;/div&gt;
    &lt;!-- End: Content --&gt;
    &lt;!-- Start: Footer --&gt;
    &lt;div ng-include="'templates/footer.html'"&gt;&lt;/div&gt;
    &lt;!-- End: Footer --&gt;


    &lt;!-- jQuery (necessary for Bootstrap's JavaScript plugins) --&gt;
    &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"&gt;&lt;/script&gt;
    &lt;!-- Include all compiled plugins (below), or include individual files as needed --&gt;
    &lt;script src="js/bootstrap.min.js"&gt;&lt;/script&gt;
    &lt;script src="js/jquery.isotope.min.js"&gt;&lt;/script&gt;
    &lt;script src="js/fancybox/jquery.fancybox.pack.js"&gt;&lt;/script&gt;
    &lt;script src="js/wow.min.js"&gt;&lt;/script&gt;

  &lt;!-- Vendor: Angular, followed by our custom Javascripts --&gt;

  &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.0/angular-route.min.js"&gt;&lt;/script&gt;

  &lt;!-- parent angular module and controller that supports SPA (Single Page Application) --&gt;
  &lt;script src="app/appMod.js"&gt;&lt;/script&gt;
  &lt;script src="app/home/HomeCtrl.js"&gt;&lt;/script&gt;
  &lt;script src="app/blog/BlogCtrl.js"&gt;&lt;/script&gt;
  &lt;script src="js/functions.js"&gt;&lt;/script&gt;
  &lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p>app/appMod.js</p>

<pre><code>var app = angular.module("appMod", ["ngRoute"])

app.factory('NavBannerService', function(){
  function _setNavBanner(NavElementItem){
    /* Change the active element to the Blog li element.  */
    var navLIs = document.querySelector('.nav-pills').childNodes;
    /* Remove all the active class from each li element. */
    for(var a = 0; a &lt;= navLIs.length-1; a++)
    {
       navLIs[a].className = "";
       alert(NavElementItem)
       if(navLIs[a].id == NavElementItem)
       {
         alert(NavElementItem)
         navLIs[a].className = "active";
       }
    }
  };

  return{
    SetNavBanner: _setNavBanner
  };
});


/* Configure the Routes*/
app.config(['$routeProvider', function ($routeProvider) {
    $routeProvider
      .when("/", { templateUrl: "app/home/home.html", controller: "HomeCtrl" })
      .when("/Blog", {templateUrl: "app/blog/blog.html", controller: "BlogCtrl"})
      .otherwise({ redirectTo: '/' });
      /*.when("/SearchByEmployees", { templateUrl: "Search/SearchByEmployees.html", controller: "SearchByEmpCtrl" })
      .when("/Procurements", { templateUrl: "Procurements/ProcurementView.html", controller: "ProcurementCtrl" })
      .when("/Participants", { templateUrl: "Participants/ParticipantsSummaryView.html", controller: "ParticipantsCtrl" })*/
}]);
</code></pre>

<p>home/HomeCtrl.js</p>

<pre><code>app.controller('HomeCtrl', ['$scope', function($scope,NavBannerService){
       NavBannerService.SetNavBanner("liHome");
}]);
</code></pre>

<p>home/home.html</p>

<pre><code>&lt;div ng-controller="HomeCtrl"&gt;
&lt;div class="slider"&gt;
  &lt;img src="img/bg-main1.jpg" alt="" class="img-responsive" /&gt;
&lt;/div&gt;
&lt;!-- column content --&gt;
&lt;div id="main-content"&gt;
  &lt;div class="container"&gt;
    &lt;div class="row"&gt;
      &lt;div class="big-box"&gt;
        &lt;div class="col-lg-12"&gt;
          &lt;div class="col-md-4"&gt;
            &lt;div class="box-bg"&gt;
              &lt;div class="wow bounceInLeft" data-wow-duration="2s" data-wow-delay="0.9s"&gt;
                &lt;div class="align-center"&gt;
                  &lt;i class="fa fa-camera-retro fa-5x"&gt;&lt;/i&gt;
                  &lt;h4 class="text-bold"&gt;Customizable&lt;/h4&gt;
                  &lt;p&gt;Lorem ipsum dolor sit amet, mutat graeco volumus ad eam, singulis patrioque comprehensam nam no.
                  Mei cu dicat voluptaria volumus.
                  &lt;/p&gt;
                &lt;/div&gt;
              &lt;/div&gt;
            &lt;/div&gt;
          &lt;/div&gt;
          &lt;div class="col-md-4"&gt;
            &lt;div class="box-bg"&gt;
              &lt;div class="wow bounceInLeft" data-wow-duration="2s" data-wow-delay="0.6s"&gt;
                &lt;div class="align-center"&gt;
                  &lt;i class="fa fa-shield fa-5x"&gt;&lt;/i&gt;
                  &lt;h4 class="text-bold"&gt;Valid HTML5&lt;/h4&gt;
                  &lt;p&gt;Lorem ipsum dolor sit amet, mutat graeco volumus ad eam, singulis patrioque comprehensam nam no.
                  Mei cu dicat voluptaria volumus.
                  &lt;/p&gt;
                &lt;/div&gt;
              &lt;/div&gt;
            &lt;/div&gt;
          &lt;/div&gt;
          &lt;div class="col-md-4"&gt;
            &lt;div class="box-bg"&gt;
              &lt;div class="wow bounceInLeft" data-wow-duration="2s" data-wow-delay="0.2s"&gt;
                &lt;div class="align-center"&gt;
                  &lt;i class="fa fa-circle fa-5x"&gt;&lt;/i&gt;
                  &lt;h4 class="text-bold"&gt;Responsive&lt;/h4&gt;
                  &lt;p&gt;Lorem ipsum dolor sit amet, mutat graeco volumus ad eam, singulis patrioque comprehensam nam no.
                  Mei cu dicat voluptaria volumus.
                  &lt;/p&gt;
                &lt;/div&gt;
              &lt;/div&gt;
            &lt;/div&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  &lt;div class="container"&gt;
    &lt;div class="row"&gt;
      &lt;div class="wow bounceInRight"&gt;
        &lt;div class="col-lg-6"&gt;
          &lt;h4&gt;Maecenas luctus&lt;/h4&gt;
          &lt;p&gt;Lorem ipsum dolor sit amet, mutat graeco volumus ad eam, singulis patrioque comprehensam nam no.
          Mei cu dicat voluptaria volumus.&lt;/p&gt;
          &lt;a href="#" class="thumbnail"&gt;
            &lt;img src="img/thumbnails/outline-1.jpg" alt=""&gt;
          &lt;/a&gt;
        &lt;/div&gt;
      &lt;/div&gt;
      &lt;div class="wow bounceInLeft"&gt;
        &lt;div class="col-lg-6"&gt;
          &lt;h4&gt;Maecenas luctus&lt;/h4&gt;
          &lt;p&gt;Lorem ipsum dolor sit amet, mutat graeco volumus ad eam, singulis patrioque comprehensam nam no.
          Mei cu dicat voluptaria volumus.&lt;/p&gt;
          &lt;a href="#" class="thumbnail"&gt;
            &lt;img src="img/thumbnails/ipad.jpg" alt=""&gt;
          &lt;/a&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;!-- end column content --&gt;
&lt;/div&gt;
</code></pre>

<p>blog/BlogCtrl.js</p>

<pre><code>app.controller('BlogCtrl', ['$scope', function($scope,NavBannerService){
   new WOW().init();

   NavBannerService.SetNavBanner("liBlog");

   //anglar.element(document).find('')
}]);
</code></pre>

<p>blog/blog.html</p>

<pre><code>&lt;div ng-controller="BlogCtrl"&gt;
    &lt;div class="row mar-bot40"&gt;
        &lt;div class="col-md-offset-3 col-md-6"&gt;
            &lt;div class="sections-header"&gt;
                &lt;h2&gt;Blog&lt;/h2&gt;
                &lt;p&gt;Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet consectetur, adipisci velit, sed quia non numquam.&lt;/p&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;section id="content"&gt;
    &lt;div class="container"&gt;
        &lt;div class="row"&gt;
            &lt;div class="col-lg-8"&gt;
                &lt;article&gt;
                    &lt;div class="post-image"&gt;
                        &lt;div class="post-heading"&gt;
                            &lt;h3&gt;&lt;a href="#"&gt;This is an example of standard post format&lt;/a&gt;&lt;/h3&gt;
                        &lt;/div&gt;
                        &lt;img src="img/portfolio/img1.jpg" alt="" /&gt;
                    &lt;/div&gt;
                    &lt;p&gt;
                    Qui ut ceteros comprehensam. Cu eos sale sanctus eligendi, id ius elitr saperet, ocurreret pertinacia pri an. No mei nibh consectetuer, semper laoreet perfecto ad qui, est rebum nulla argumentum ei. Fierent adipisci iracundia est ei, usu timeam persius ea. Usu ea justo malis, pri quando everti electram ei, ex homero omittam salutatus sed.
                    &lt;/p&gt;
                    &lt;div class="bottom-article"&gt;
                        &lt;ul class="meta-post"&gt;
                            &lt;li&gt;&lt;i class="icon-calendar"&gt;&lt;/i&gt;&lt;a href="#"&gt; Mar 23, 2013&lt;/a&gt;&lt;/li&gt;
                            &lt;li&gt;&lt;i class="icon-user"&gt;&lt;/i&gt;&lt;a href="#"&gt; Admin&lt;/a&gt;&lt;/li&gt;
                            &lt;li&gt;&lt;i class="icon-folder-open"&gt;&lt;/i&gt;&lt;a href="#"&gt; Blog&lt;/a&gt;&lt;/li&gt;
                            &lt;li&gt;&lt;i class="icon-comments"&gt;&lt;/i&gt;&lt;a href="#"&gt;4 Comments&lt;/a&gt;&lt;/li&gt;
                        &lt;/ul&gt;
                        &lt;a href="#" class="pull-right"&gt;Continue reading &lt;i class="icon-angle-right"&gt;&lt;/i&gt;&lt;/a&gt;
                    &lt;/div&gt;
                &lt;/article&gt;
                &lt;article&gt;
                    &lt;div class="post-video"&gt;
                        &lt;div class="post-heading"&gt;
                            &lt;h3&gt;&lt;a href="#"&gt;Amazing video post format here&lt;/a&gt;&lt;/h3&gt;
                        &lt;/div&gt;
                        &lt;div class="video-container"&gt;
                            &lt;iframe src="http://player.vimeo.com/video/30585464?title=0&amp;amp;byline=0"&gt;
                            &lt;/iframe&gt;
                        &lt;/div&gt;
                    &lt;/div&gt;
                    &lt;p&gt;
                         Qui ut ceteros comprehensam. Cu eos sale sanctus eligendi, id ius elitr saperet, ocurreret pertinacia pri an. No mei nibh consectetuer, semper laoreet perfecto ad qui, est rebum nulla argumentum ei. Fierent adipisci iracundia est ei, usu timeam persius ea. Usu ea justo malis, pri quando everti electram ei.
                    &lt;/p&gt;
                    &lt;div class="bottom-article"&gt;
                        &lt;ul class="meta-post"&gt;
                            &lt;li&gt;&lt;i class="icon-calendar"&gt;&lt;/i&gt;&lt;a href="#"&gt; Mar 23, 2013&lt;/a&gt;&lt;/li&gt;
                            &lt;li&gt;&lt;i class="icon-user"&gt;&lt;/i&gt;&lt;a href="#"&gt; Admin&lt;/a&gt;&lt;/li&gt;
                            &lt;li&gt;&lt;i class="icon-folder-open"&gt;&lt;/i&gt;&lt;a href="#"&gt; Blog&lt;/a&gt;&lt;/li&gt;
                            &lt;li&gt;&lt;i class="icon-comments"&gt;&lt;/i&gt;&lt;a href="#"&gt;4 Comments&lt;/a&gt;&lt;/li&gt;
                        &lt;/ul&gt;
                        &lt;a href="#" class="pull-right"&gt;Continue reading &lt;i class="icon-angle-right"&gt;&lt;/i&gt;&lt;/a&gt;
                    &lt;/div&gt;
                &lt;/article&gt;
                &lt;div id="pagination"&gt;
                    &lt;span class="all"&gt;Page 1 of 3&lt;/span&gt;
                    &lt;span class="current"&gt;1&lt;/span&gt;
                    &lt;a href="#" class="inactive"&gt;2&lt;/a&gt;
                    &lt;a href="#" class="inactive"&gt;3&lt;/a&gt;
                &lt;/div&gt;
            &lt;/div&gt;
            &lt;div class="col-lg-4"&gt;
                &lt;aside class="right-sidebar"&gt;
                &lt;div class="widget"&gt;
                    &lt;form class="form-search"&gt;
                        &lt;input class="form-control" type="text" placeholder="Search.."&gt;
                    &lt;/form&gt;
                &lt;/div&gt;
                &lt;div class="widget"&gt;
                    &lt;h5 class="widgetheading"&gt;Categories&lt;/h5&gt;
                    &lt;ul class="cat"&gt;
                        &lt;li&gt;&lt;i class="icon-angle-right"&gt;&lt;/i&gt;&lt;a href="#"&gt;Web design&lt;/a&gt;&lt;span&gt; (20)&lt;/span&gt;&lt;/li&gt;
                        &lt;li&gt;&lt;i class="icon-angle-right"&gt;&lt;/i&gt;&lt;a href="#"&gt;Online business&lt;/a&gt;&lt;span&gt; (11)&lt;/span&gt;&lt;/li&gt;
                        &lt;li&gt;&lt;i class="icon-angle-right"&gt;&lt;/i&gt;&lt;a href="#"&gt;Marketing strategy&lt;/a&gt;&lt;span&gt; (9)&lt;/span&gt;&lt;/li&gt;
                        &lt;li&gt;&lt;i class="icon-angle-right"&gt;&lt;/i&gt;&lt;a href="#"&gt;Technology&lt;/a&gt;&lt;span&gt; (12)&lt;/span&gt;&lt;/li&gt;
                        &lt;li&gt;&lt;i class="icon-angle-right"&gt;&lt;/i&gt;&lt;a href="#"&gt;About finance&lt;/a&gt;&lt;span&gt; (18)&lt;/span&gt;&lt;/li&gt;
                    &lt;/ul&gt;
                &lt;/div&gt;
                &lt;div class="widget"&gt;
                    &lt;h5 class="widgetheading"&gt;Latest posts&lt;/h5&gt;
                    &lt;ul class="recent"&gt;
                        &lt;li&gt;
                        &lt;img src="img/blog/1.jpg" class="pull-left" alt="" /&gt;
                        &lt;h6&gt;&lt;a href="#"&gt;Lorem ipsum dolor sit&lt;/a&gt;&lt;/h6&gt;
                        &lt;p&gt;
                        Mazim alienum appellantur eu cu ullum officiis pro pri
                        &lt;/p&gt;
                        &lt;/li&gt;
                        &lt;li&gt;
                        &lt;a href="#"&gt;&lt;img src="img/blog/2.jpg" class="pull-left" alt="" /&gt;&lt;/a&gt;
                        &lt;h6&gt;&lt;a href="#"&gt;Maiorum ponderum eum&lt;/a&gt;&lt;/h6&gt;
                        &lt;p&gt;
                        Mazim alienum appellantur eu cu ullum officiis pro pri
                        &lt;/p&gt;
                        &lt;/li&gt;
                        &lt;li&gt;
                        &lt;a href="#"&gt;&lt;img src="img/blog/3.jpg" class="pull-left" alt="" /&gt;&lt;/a&gt;
                        &lt;h6&gt;&lt;a href="#"&gt;Et mei iusto dolorum&lt;/a&gt;&lt;/h6&gt;
                        &lt;p&gt;
                        Mazim alienum appellantur eu cu ullum officiis pro pri
                        &lt;/p&gt;
                        &lt;/li&gt;
                    &lt;/ul&gt;
                &lt;/div&gt;
                &lt;div class="widget"&gt;
                    &lt;h5 class="widgetheading"&gt;Popular tags&lt;/h5&gt;
                    &lt;ul class="tags"&gt;
                        &lt;li&gt;&lt;a href="#"&gt;Web design&lt;/a&gt;&lt;/li&gt;
                        &lt;li&gt;&lt;a href="#"&gt;Trends&lt;/a&gt;&lt;/li&gt;
                        &lt;li&gt;&lt;a href="#"&gt;Technology&lt;/a&gt;&lt;/li&gt;
                        &lt;li&gt;&lt;a href="#"&gt;Internet&lt;/a&gt;&lt;/li&gt;
                        &lt;li&gt;&lt;a href="#"&gt;Tutorial&lt;/a&gt;&lt;/li&gt;
                        &lt;li&gt;&lt;a href="#"&gt;Development&lt;/a&gt;&lt;/li&gt;
                    &lt;/ul&gt;
                &lt;/div&gt;
                &lt;/aside&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;/section&gt;
  &lt;/div&gt;
</code></pre>

<p><strong>EDIT:</strong>
After updating my factor service this, then it worked:</p>

<pre><code>app.factory('NavBannerService', function(){
  var SetNavBanner = function(NavElementItem){
    /* Change the active element to the Blog li element.*/
    var navLIs = document.querySelector('.nav-pills').childNodes;
    /* Remove all the active class from each li element. */
    for(var a = 0; a &lt;= navLIs.length-1; a++)
    {
       navLIs[a].className = "";

       if(navLIs[a].id == NavElementItem)
       {

         navLIs[a].className = "active";
       }
    }

  };
  return {SetNavBanner: SetNavBanner};
});
</code></pre>

<p>Why would that work, instead of this?</p>

<pre><code>app.factory('NavBannerService', function(){
  this. SetNavBanner = function(NavElementItem){
    /* Change the active element to the Blog li element.*/
    var navLIs = document.querySelector('.nav-pills').childNodes;
    /* Remove all the active class from each li element. */
    for(var a = 0; a &lt;= navLIs.length-1; a++)
    {
       navLIs[a].className = "";

       if(navLIs[a].id == NavElementItem)
       {

         navLIs[a].className = "active";
       }
    }

  };

});
</code></pre>