繁体   English   中英

AngularJS ReferenceError:未定义$ window

[英]AngularJS ReferenceError: $window is not defined

如果他们通过我的表单验证(根据数据库值检查用户名和密码),我试图重新指导我的用户。

验证工作正常,但在我的.Success函数中,重定向似乎不起作用,它产生错误:'ReferenceError:$ window not defined'。

这是代码:

.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');       
    }
});

我试过改变位置路径,但似乎没有任何工作。 有任何想法吗?

谢谢!

LazyTotoro

$window需要注入。

要注入它,只需将其作为参数添加到控制器功能中,Angular将自动处理其余部分。

例如:

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

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

您可以在此处阅读有关AngularJS中依赖项注入的更多信息。

如果您不需要重新加载整页,则应该注入并使用$ 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