2
angular.module('site.controllers', ['ngCookies'])
    .controller("LoginController", ["$scope", "$http", "$cookies", function($scope, $http, $cookies){
        debugger; // can access $cookies here
        $scope.login = function(){
            debugger; // ReferenceError: $cookies is not defined
            var credentials = {
                'username': $scope.username,
                'password': $scope.password
            };
            $http.post('/login', credentials)
                .success( function(data, status, headers, config){
                    $scope.template = $scope.templates[2];
                })
                .error( function(data, status, headers, config){
                    debugger;
                    $scope.template = $scope.templates[2];
                //TODO: info for user that login failed
                });
        };
    }])
;

I have no idea why $cookies service is available in LoginController direct body, but it is not available in LoginController's login function.

$scope and $http are available in both places, however the $cookies service is problematic.

These files are included in HTML head:

<script type="text/javascript" src="static/lib/angular/angular.js"></script>
<script type="text/javascript" src="static/lib/angular/angular-cookies.js"></script>

1 Answer 1

4

I think it is accessible within the login method, as long you use it. If you try to see the data by just using debugger, then you would not get it in debugging console. This i think is because the js engine did not create closure since you had not referenced the property within the login method.

Try to do console.log($cookies) inside login method.

Sign up to request clarification or add additional context in comments.

3 Comments

You're right. When I use $cookies within login method I can access it in debugger.
OMG, thank you! I thought I was losing my freaking mind. =)
@Chandermani - You are GEM. I was about to throw my laptop away :)

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.