1

I have a loginController.js,loginService.js,app.js and a view page

app.js

var validation = angular.module('validationApp', ['ui.router','ngDialog','ngSanitize','angularSpinner']);
validation.config(function ($stateProvider, $urlRouterProvider) {
    $urlRouterProvider.otherwise('/login');
    $stateProvider
    .state('login', {
        url : '/login',
        views: {
            "mainView": {
               controller: 'loginController',
               templateUrl: '/public/app/login/loginView.phtml'
            },
        }
    }).run(['$rootScope', '$location', '$http', function ($rootScope, $location, $http) {
    
    $rootScope.globals = JSON.parse(localStorage.getItem('globals')) || {};
    if ($rootScope.globals.currentUser) {
        $http.defaults.headers.common['Authorization'] = 'Basic ' + $rootScope.globals.currentUser.authdata;
    }
    $rootScope.$on('$locationChangeStart', function (event, next, current) {

        var loggedIn = $rootScope.globals.currentUser;
        if (!loggedIn) {
            $location.path('/login');
        } else if (loggedIn && $location.path() == '/login') {
            $location.path('/validation');
        }
    });
    
}]);

loginService.js

app.factory('loginService', ['$http', '$rootScope', function ($http, $rootScope) {
    var service = {};
    
    service.Login            = Login;
    service.SetCredentials   = SetCredentials;
    service.ClearCredentials = ClearCredentials;

    return service;
  function Login(user, callback)
    {
        var request = $http({
            method  : "post",
            url     : "/app/index/login",
            data    : {
                        userEmail: user.userEmail,
                        password: user.password
                      }
        }).success(function (response, status, headers, config) {
            callback(response);
        });
    };

loginController.js

validation.controller('loginController',['$scope','$http', '$location','loginService',function($scope,$http,$location,loginService) {
			
    
        $scope.submitForm = function(user) {

        $http({
            method: 'post',
            url: '/index.php/home/create',
            data: {
                username:$scope.user.userName,
                password:$scope.user.password,
            },
            headers: {'Content-Type': 'application/x-www-form-urlencoded'}
		}).success(function(response){
                     loginService.SetCredentials(response.userData);
                    ..............................

while executing my code i am getting an error like :

1.app is not defined in loginService.js

2.Error: $injector:unpr Unknown Provider Unknown provider: loginServiceProvider <- loginService <- loginController

I am new in angularjs.so please help me to solve this.

3
  • 2
    replace app with validation in your loginService.js @student Commented Jan 5, 2017 at 7:27
  • @student you might want to consider not use this approach! You should better ask angular for you ngApp in each of your files, to remove the issue with a variable in the global scope. Have a look at my answer or the one of Giovani. Commented Jan 5, 2017 at 7:35
  • ok.i will try that one also Commented Jan 5, 2017 at 7:37

4 Answers 4

3

A more sofisticated aproach would be to get the ngApp in each file. This removes the issues you have at the moment with not referencing the correct global variable ('don't polute the global scope' is a statement you might want to consider as well).

angular.module('validationApp').controller(...)

or

angular.module('validationApp').factory(...)
Sign up to request clarification or add additional context in comments.

Comments

1

1.app is not defined in loginService.js

Exactly what it says, you're using the app variable, but it isn't defined in loginService (or in any of the other js files).

Replace the following in loginService.js

app.factory

with

angular.module('validationApp').factory

2.Error: $injector:unpr Unknown Provider Unknown provider: loginServiceProvider <- loginService <- loginController

This error is caused because of the mistake you made in loginService.js

Comments

0

You should replace validation.factory instead of app.factory

Comments

0

Change

FROM

app.factory('loginService', ['$http', '$rootScope', function ($http, $rootScope) {

TO

validation.factory('loginService', ['$http', '$rootScope', function ($http, $rootScope) {

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.