Join the Stack Overflow Community
Stack Overflow is a community of 6.5 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I'm a beginner with Angularjs, and I have some difficulties understanding modules and scopes.

I keep getting the error that the scope is undefined, but I don't get why. First I had my controller linked where I set my route, but since the function inside the controller is called on submit button click I took it away. I've tried putting it back, but that didn't make any difference.

This is my js file:

var myApp = angular.module('myApp', ['ngRoute']);

// routes
myApp.config(function($routeProvider) {
    $routeProvider

        // route for the home page
        .when('/', {
            templateUrl : 'home.html',
            controller  : 'mainController'
        })

        // route for the about page
        .when('/about', {
            templateUrl : 'about.html',
            controller  : 'aboutController'
        })

        // route for the login page
        .when('/login', {
            templateUrl : 'login.html'
        });
});


myApp.controller('mainController', function($scope) {
    $scope.message = 'Beginpagina';
});

myApp.controller('aboutController', function($scope) {
    $scope.message = 'Informatie over deze pagina';
});


function loginCtrl($scope, $http){
    $scope.doLogin = function() {

    $http({

                method: 'POST', 
                url: 'loginController.php',
                headers: {'Content-Type': 'application/x-www-form-urlencoded'},
                data: { 
                        'username': $scope.un, 
                        'password': $scope.pw 
                     },
            }).

            success(function(data, status) {

                $scope.data = data;
                if(data == 'FALSE'){
                    $scope.errorMessage = 'Geen geldige inloggegevens';
                } else {
                   scope.$apply(function() { $location.path("/profile"); });
                }

            }).

            error(function(data, status) {
                $scope.data = data || "FALSE";
                $scope.errorMessage = 'Something went wrong';
            });
    };
};

And this is my login-page where I get the error, trying to log in:

<div class="span5" ng-controller="loginCtrl"> 
    <form>
       <fieldset>
              <div class="form-group">
                <input class="form-control" placeholder="Gebruikersnaam" ng-model="un" 
                  type="text" autocomplete="off">
               </div>
               <div class="form-group">
                  <input class="form-control" placeholder="Password" ng-model="pw"  
                    type="password" value="" autocomplete="off">
               </div>
               <input class="btn btn-lg btn-success btn-block" type="submit" 
                 ng-click="doLogin()" value="Login">
               <div>{{errorMessage}}</div>
           </fieldset>
       </form>
   </div>

I don't know if I should post the index-page as well, since the routing for home/about page works fine, so the problem is somewhere making the logincontroller and linking it to my submit button. I have found some similar questions, but I haven't seen anyone mix a new controller with myApp controllers, so I might be doing it completely wrong. I've also read that one html page can only have one module, so I didn't know if I could put the login controller apart. It would be nice to have some more info to be put in the right direction. Thanks in advance!

share|improve this question
    
As @Satpal has pointed out the error comes from the fact you are referencing scope instead of scope. However, you don't need the $scope.$apply() there either. The success callback for the promise will already be in a $digest. – Davin Tryon Jan 10 '14 at 17:19
    
Ok.. Kind of embarrassed :) Just proves that I need to take a break.. And how would I go about routing to a page upon a succesful login, because $location seems to be undefined as well :/ – Iris Van de Zandschulp Jan 10 '14 at 17:21
up vote 7 down vote accepted

In loginCtrl, you have used scope instead of $scope

Problem is in line

  scope.$apply(function() 

Use

  $scope.$apply(function() 
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.