AngularJS ReferenceError: $window未定义

时间:2022-11-06 14:30:49

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未定义”。

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

1 个解决方案

#1


43  

$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.

要注入它,你只需将它作为一个参数添加到你的控制器函数中,角就会自动处理剩下的部分。

For example:

例如:

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

    $window.location = 'http://*.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:

如果你不需要全页重载,你应该注入并使用$location:

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

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

#1


43  

$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.

要注入它,你只需将它作为一个参数添加到你的控制器函数中,角就会自动处理剩下的部分。

For example:

例如:

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

    $window.location = 'http://*.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:

如果你不需要全页重载,你应该注入并使用$location:

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

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