AngularJS ReferenceError:未定义$ window [英] AngularJS ReferenceError: $window is not defined

查看:66
本文介绍了AngularJS ReferenceError:未定义$ window的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果用户通过我的表单验证(我要对照数据库值检查用户名和密码),我将尝试重定向他们.

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

验证工作正常,但是在我的.Success函数中,重定向似乎无法正常工作,它会产生错误:"ReferenceError:未定义$ window".

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(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?

谢谢!

LazyTotoro

LazyTotoro

推荐答案

$window需要注入.

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

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

例如:

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

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

您可以在AngularJS中的此处中了解更多有关依赖项注入的信息.

You can read more about dependency injection in AngularJS here.

如果不需要重新加载整个页面,则应注入并使用 $ location :

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

这篇关于AngularJS ReferenceError:未定义$ window的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆