Charkizard Charkizard - 4 months ago 99x
AngularJS Question

AngularJS ReferenceError: $window is not defined

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) {

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;

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




$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 = ''

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

// change the path