简体   繁体   English

AngularJS ReferenceError:未定义$ window

[英]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'. 验证工作正常,但在我的.Success函数中,重定向似乎不起作用,它产生错误:'ReferenceError:$ window 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 LazyTotoro

$window needs to be injected. $window需要注入。

To inject it you simply add it as a parameter to your controller function and Angular will automatically take care of the rest. 要注入它,只需将其作为参数添加到控制器功能中,Angular将自动处理其余部分。

For example: 例如:

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

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

You can read more about dependency injection in AngularJS here . 您可以在此处阅读有关AngularJS中依赖项注入的更多信息。

If you don't need a full page reload you should instead inject and use $location : 如果您不需要重新加载整页,则应该注入并使用$ location

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

// change the path
$location.path('/newValue');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM