Error: $routeProvider is not defined

Hello,

I am working on a login form with Ionic and AngularJS and when i click on Connect, I always have an error message showing : $routeProvider is not defined

Hers’s the sources :

This is the Index.html

<!doctype html>
<html lang="en" ng-app="GroupePrive">
<head>
  <meta charset="utf-8">
  <title>Groupe Privé</title>
  <link rel="stylesheet" href="css/app.css"/>
</head>
<body>
  <div ng-view></div>
  <!-- In production use:
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
  -->
  
  <script src="lib/angular/angular.js"></script>
  <script src="lib/angular/angular-route.js"></script>
  <script src="js/app.js"></script>
  <script src="js/directives/loginDrc.js"></script>
  <script src="js/controllers/loginCtrl.js"></script>
  <script src="js/services/loginService.js"></script>
</body>
</html>

App.js :

'use strict';
// Declare app level module which depends on filters, and services
var app= angular.module('GroupePrive', ['ngRoute']);
app.config(['$routeProvider', function($routeProvider) {
  $routeProvider.when('/login', {templateUrl: 'partials/login.html', controller: 'loginCtrl'});
  $routeProvider.otherwise({redirectTo: '/login'});
}]);

LoginService.js

'use strict';
app.factory('loginService',function($http){
    return{
        login:function(data,scope){
            var $promise=$http.post('data/user.php',data); //send data to user.php
            $promise.then(function(msg){
                if(msg.data=='succes') {
                    scope.msgtxt='Informations correctes';
                    console.log('login succes');
                }
                else {
                    scope.msgtxt='Informations incorrectes';
                    console.log('login error');
                }                     
            });
            $routeProvider.otherwise({redirectTo: '/login'});
        }
    }   
});

user.php :

<?php 
    $user=json_decode(file_get_contents('php://input'));  //Accesseur sur user
    if($user->mail=='mouhri.amine@gmail.com' && $user->pass=='1234') 
        print 'succes';
    else 
        print 'error';

?>

Thank you a lot for helping me !

You should add $routeProvider as factory parameter in your LoginService.js.

app.factory('loginService',function($http, $routeProvider)

It doesn’t work :confused:

I have this
Error: [$injector:unpr] Unknown provider: $routeProviderProvider <- $routeProvider <- loginService

Okay, why you need to use $routeProvider in your LoginService.js? What’s the reason?
Or can you show your loginCtrl.js?

here’s the loginCtrl.js

'use strict';
 
app.controller('loginCtrl', ['$scope','loginService', function ($scope,loginService) {
    $scope.msgtxt='';
    $scope.login=function(user){
        loginService.login(user,$scope); //appeler le service d'authentificiation
    };
}]);

Try this:

loginCtrl.js

    app.controller('loginCtrl', ['$scope','loginService', function ($scope,loginService) {
          $scope.msgtxt='';
          $scope.login=function(user){
              loginService.login(user)
                    .then(function( msg ){
                         if(msg.data=='succes') {
                            $scope.msgtxt='Informations correctes';
                            console.log('login succes');
                         }
                        else {
                            $scope.msgtxt='Informations incorrectes';
                            console.log('login error');
                        }     
                     });
        };
    }]);

LoginService.js:

    app.factory('loginService',function($http){
    return{
        login:function(data,scope){
            return $http.post('data/user.php',data); //send data to user.php
        }
    }   
});
1 Like

It works !!

Thank you a lot :wink: