javascript AngularJS,$window 没有定义,怎么办?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23317410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 00:43:00  来源:igfitidea点击:

AngularJS, $window is not defined, how to?

javascriptangularjswindowurl-redirection

提问by Patrioticcow

i am trying to do a redirect on a click

我正在尝试在单击时进行重定向

.controller('TestCtrl', function ($scope, $stateParams) {
    document.getElementById('next_question').addEventListener('click', function (e) {
        $window.location.href = '#/tab/category/1';
        return true;
    }, false);
})

but i get $window is not defined. So... how would i get the $windowor do a redirect a different way

但我明白了$window is not defined。所以......我将如何获得$window或以不同的方式进行重定向

any ideas?

有任何想法吗?

回答by Anthony Chu

$window needs to be included as an argument so that it can be injected...

$window 需要作为参数包含在内,以便它可以被注入...

.controller('TestCtrl', function ($scope, $stateParams, $window) {
    document.getElementById('next_question').addEventListener('click', function (e) {
        $window.location.href = '#/tab/category/1';
        return true;
    }, false);
})