Html $window.location.href 在 AngularJS 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34017904/
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-08-29 18:34:50  来源:igfitidea点击:

$window.location.href NOT WORKING in AngularJS

htmlangularjsloginwindow.location

提问by Varun Nair

I'm building a basic AngularJSLogin page and the $window.location.hrefdid not re-direct the page to a new html in my system, hosted by WAMP. I even tried re-directing it to google. Nothing happens. I tried all the available solutions here and nothing seems to work. Any solutions ?

JS followed by HTML

我正在构建一个基本的AngularJS登录页面,并且$window.location.href没有将该页面重定向到我系统中由 WAMP 托管的新 html。我什至尝试将其重定向到谷歌。没发生什么事。我在这里尝试了所有可用的解决方案,但似乎没有任何效果。任何解决方案?

JS 后跟 HTML

var app = angular.module('myApp', []);

app.controller('MainCtrl', function($scope) {
  $scope.name = 'World';


  $scope.submit = function() {
    if ($scope.username && $scope.password) {
      var user = $scope.username;
      var pass = $scope.password;
      if (pass == "admin" && user == "[email protected]") {
        alert("Login Successful");
        $window.location.href = "http://google.com"; //Re-direction to some page
      } else if (user != "[email protected]") {
        alert("Invalid username");
      } else if (pass != "admin" && user == "[email protected]") {
        alert("Invalid password");
      }
    } else {
      alert("Invalid Login");
    }
  }


});
<!DOCTYPE html>
<html ng-app="myApp">

<head>
  <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
  <script src="login.js"></script>
  <link rel="stylesheet" href="login.css">
</head>

<body ng-controller="MainCtrl">
  <form>
    <label>Username:</label>
    <input type="email" ng-model="username" class="lab1" />
    </br>
    </br>
    <label></label>Password:</label>
    <input type="password" ng-model="password" class="lab2">
    </br>
    <button type="submit" ng-click="submit()" class="buttonclass">login</button>
  </form>
</body>

</html>

采纳答案by Alexandre

In angular js you can use $location. Inject it in your controller :

在 Angular js 中,您可以使用$location. 将其注入您的控制器:

app.controller('MainCtrl', function($scope, $location) { ... }

And if you want to redirect to google use its url()method :

如果你想重定向到谷歌使用它的url()方法:

$location.url('http://google.fr');

you can also use path()method for relative url :

您还可以使用path()相对 url 的方法:

$location.path('home'); // will redirect you to 'yourDomain.xx/home'

https://docs.angularjs.org/api/ng/service/$location

https://docs.angularjs.org/api/ng/service/$location

回答by Jim Grimmett

It is worth noting the current documentationfor $location. Specifically the quote:

值得注意的是,在当前文档$location。具体报价如下:

When should I use $location?

Any time your application needs to react to a change in the current URL or if you want to change the current URL in the browser.

What does it not do?

It does not cause a full page reload when the browser URL is changed. To reload the page after changing the URL, use the lower-level API, $window.location.href.

我什么时候应该使用 $location?

任何时候您的应用程序需要对当前 URL 的更改做出反应,或者如果您想更改浏览器中的当前 URL。

它不做什么?

当浏览器 URL 更改时,它不会导致整个页面重新加载。要在更改 URL 后重新加载页面,请使用较低级别的 API,$window.location.href。

If you need to redirect the browser to a new page, $window.location is definitely recommended.

如果需要将浏览器重定向到新页面,绝对推荐使用 $window.location。

回答by Ramu Vemula

Try this,

var homeApp = angular.module('HomeApp', []);

homeApp.controller('HomeController', function ($scope, $http, $window) {
    $scope.loginname = "Login To Demo App";
    $scope.login = function () {

        var name = $scope.username;
        var pwd = $scope.password;
        var req = {
            method: 'POST',
            url: '../Account/LoginAccount',
            headers: {
                'Content-Type': undefined
            },
            params: { username: name, password: pwd }
        }

        $http(req).then(function (responce) {

            var url = '';
            if (responce.data == "True") {
                url = '../Account/WelcomePage';
                $window.location.href = url;
            }
            else {
                url = '../Account/Login';
                $window.location.href = url;
            }
        }, function (responce) {

        });
    }
});

回答by ?smail ?ener

Angular has its own location handler named $locationwhich I prefer to use in angular app;

Angular 有自己的位置处理程序$location,我更喜欢在 angular 应用程序中使用它;

app.controller('MainCtrl', function($scope, $location) {

inject to your controller and use as following;

注入您的控制器并按如下方式使用;

$location.path('http://foo.bar')

回答by Miguel Herreros Cejas

You can use $window.location.hrefin AngularJS

您可以$window.location.href在 AngularJS 中使用

app.controller('MainCtrl', function ($scope,$window) {
    $scope.formData = {};
    $scope.submitForm = function (formData){
    $window.location.href = "jobs";
    };
  })

回答by Justin Kruse

My two cents -

我的两分钱——

I could not get $window.location.hrefor $location.pathto work to navigate away from a page. In the documentation for $location(under caveats) it says:

我无法离开$window.location.href$location.path工作以离开页面。在$location(在警告下)的文档中,它说:

The $location service allows you to change only the URL; it does not allow you to reload the page. When you need to change the URL and reload the page or navigate to a different page, please use a lower level API, $window.location.href.

$location 服务只允许您更改 URL;它不允许您重新加载页面。当您需要更改 URL 并重新加载页面或导航到其他页面时,请使用较低级别的 API,$window.location.href。

$location Caveats

$location 注意事项

Documentation for $window is... lacking. Regardless, neither of the services worked for me in the controller (though $location.path works in my index.runfile).

$window 的文档......缺乏。无论如何,这两个服务都没有在控制器中为我工作(尽管 $location.path 在我的index.run文件中工作)。

In this project I am using ui-router, so doing this worked: $state.go('state.name');-- where state.name is the string of the state/page name to which the user wants to go, i.e. 'index.users'

在这个项目中,我使用 ui-router,所以这样做是有效的: $state.go('state.name');-- 其中 state.name 是用户想要去的状态/页面名称的字符串,即'index.users'