0

I am new to AngularJs, i have seen enough posts for a similar question but haven't found solution for my problem. I am using Angular 1.4.5

app.js (Omitted additional code like Routes and interceptors):

'use strict';

var app = angular.module('app', ['ngRoute', 'authControllers', 'authServices']);
var authControllers = angular.module('authControllers', []);
var authServices = angular.module('authServices', []);

authControllers.js:

authControllers.controller('authCtrl', ['$scope', '$location', '$window', 'UserService', 'AuthenticationService',
function authCtrl($scope, $location, $window, UserService, AuthenticationService) {
  //Admin User Controller (login, logout)
  $scope.logIn = function logIn(username, password) {
    if (username !== undefined && password !== undefined) {
      UserService.logIn(username, password).success(function(data) {
        AuthenticationService.isLogged = true;
        $window.sessionStorage.token = data.token;
        $location.path("/");
      }).error(function(status, data) {
        console.log(status);
        console.log(data);
      });
    }
  }
  $scope.logout = function logout() {
    if (AuthenticationService.isLogged) {
      AuthenticationService.isLogged = false;
      delete $window.sessionStorage.token;
      $location.path("/");
    }
  }
}
]);

authServices.js:

authServices.factory('AuthenticationService',function() {
  var auth = {
    isLogged: false
  }
  return auth;
});

Index.html:

<body ng-app="app">
    <div class="navbar navbar-inverse navbar-fixed-top" role="navigation" data-ng-controller="authCtrl">
       <!-- data-ng-controller="authCtrl" -->
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target=".navbar-collapse">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="#">Angular Restful Auth</a>
            </div>
            <div class="navbar-collapse collapse">
                <ul class="nav navbar-nav">
                    <li><a ng-href="#/">Home</a></li>
                    <li data-ng-show="token"><a ng-href="#/me">Me</a></li>
                    <li data-ng-hide="token"><a ng-href="#/signin">Signin</a></li>
                    <li data-ng-hide="token"><a ng-href="#/signup">Signup</a></li>
                    <li data-ng-show="token"><a ng-click="logout()">Logout</a></li>
                </ul>
            </div><!--/.nav-collapse -->
        </div>
    </div>
    <div class="container" ng-view="">
    </div> <!-- /container -->    
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.0-beta.1/angular-route.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-alpha1/jquery.min.js"></script>    
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script src="/js/app.js"></script>
<script src="/js/authentication/authControllers.js"></script>
<script src="/js/authentication/authServices.js"></script>
</body>

Getting Uncaught Error: [$injector:unpr] http://errors.angularjs.org/1.4.5/$injector/unpr?p0=AuthenticationServiceProvider%20%3C-%20AuthenticationService

Please let me know what's the mistake i am doing.

9
  • 1
    Did you include authController.js and authServices.js in your Index.html? Commented Oct 23, 2015 at 18:21
  • Included app.js that has references to my authControllers.js and authServices.js. Do i have to include both of them as well ? Commented Oct 23, 2015 at 18:22
  • 1
    Yep unless you are using some lazy loading library. Commented Oct 23, 2015 at 18:23
  • Still getting the same error Commented Oct 23, 2015 at 18:38
  • Can you update your original post to show what you have added? Commented Oct 23, 2015 at 18:40

1 Answer 1

1

Noting down answer as discussed in comments:

The problem was not including the modular js files in index.html

Example:

<script src="/js/authControllers.js"></script>
<script src="/js/authServices.js"></script>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.