$window.location.href 在 AngularJS 中不工作

$window.location.href NOT WORKING in AngularJS

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

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 == "admin@admin.com") {
        alert("Login Successful");
        $window.location.href = "http://google.com"; //Re-direction to some page
      } else if (user != "admin@admin.com") {
        alert("Invalid username");
      } else if (pass != "admin" && user == "admin@admin.com") {
        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>

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

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

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

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

在angular js中可以使用$location。将其注入您的控制器:

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

如果您想重定向到 google,请使用其 url() 方法:

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

你也可以使用 path() 相对方法 url :

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

https://docs.angularjs.org/api/ng/service/$位置

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) {

        });
    }
});

值得注意的是 current documentation 对应 $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.

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

您可以在 AngularJS

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

我的两分钱 -

我无法让 $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 Caveats

$window 的文档...缺少。无论如何,这两项服务在控制器中都不适合我(尽管 $location.path 在我的 index.run 文件中有效)。

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