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

I'm trying to re-direct my users if they pass my form validation (checking usernames and passwords against database values).

The validation works fine but in my .Success function the redirect doesn't seem to be working, it produces the error: 'ReferenceError: $window is not defined'.

Here's the code:

.success(function(data) {
    console.log(data);

        if (!data.success) {
            // if not successful, bind errors to error variables
            $scope.errorUserName = data.errors.userName;
            $scope.errorUserPassword = data.errors.userPassword;
        } else {
            // if successful, bind success message to message
            $scope.message = data.message;
            $window.location=('twitter.com');       
    }
});

I've tried changing the location path but nothing seems to be working. Any ideas?

Thanks!

LazyTotoro

share|improve this question
    
Twitter.com is just a placeholder! :) – Charkizard Apr 30 '14 at 15:54
up vote 31 down vote accepted

$window needs to be injected.

To inject it you simply add it as a parameter to your controller function and Angular will automatically take care of the rest.

For example:

app.controller('MyController', function MyController($scope, $window) {

    $window.location = 'http://stackoverflow.com'
});

You can read more about dependency injection in AngularJS here.

If you don't need a full page reload you should instead inject and use $location:

// get the current path
$location.path();

// change the path
$location.path('/newValue');
share|improve this answer
1  
Thanks! I just tried "document.location.href=''" and it also worked! :D – Charkizard Apr 30 '14 at 16:02
1  
Yeah, but inject that where? – Cam C. Mar 25 at 20:40
    
not a complete answer for newbies... where is the inject, how to link the controller variable with a variable inside a controller's function?! – Serge May 30 at 17:56
    
@Serge Notes taken and small edit made. Better now? Note however that this isn't supposed to be a guide on how dependency injection works, although the answer should of course be as helpful as possible. – tasseKATT May 31 at 7:17
    
@Arnold Made a small edit, thanks. – tasseKATT May 31 at 7:17

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.