如何使用AngularJS重定向到另一个页面?

Far*_*san 161 javascript angularjs

我正在使用ajax调用来执行服务文件中的功能,如果响应成功,我想将页面重定向到另一个URL.目前,我通过使用简单的js"window.location = response ['message'];"来做到这一点.但我需要用angularjs代码替换它.我在stackoverflow上看了各种解决方案,他们使用了$ location.但我是棱角分明并且很难实现它.

$http({
            url: RootURL+'app-code/common.service.php',
            method: "POST",
            headers: {'Content-Type': 'application/x-www-form-urlencoded'},
            dataType: 'json',
            data:data + '&method=signin'

        }).success(function (response) {

            console.log(response);

            if (response['code'] == '420') {

                $scope.message = response['message'];
                $scope.loginPassword = '';
            }
            else if (response['code'] != '200'){

                $scope.message = response['message'];
                $scope.loginPassword = '';
            }
            else {
                window.location = response['message'];
            }
            //  $scope.users = data.users;    // assign  $scope.persons here as promise is resolved here
        })
Run Code Online (Sandbox Code Playgroud)

Ewa*_*ger 219

你可以使用Angular $window:

$window.location.href = '/index.html';
Run Code Online (Sandbox Code Playgroud)

控制器中的示例用法:

(function () {
    'use strict';

    angular
        .module('app')
        .controller('LoginCtrl', LoginCtrl);

    LoginCtrl.$inject = ['$window', 'loginSrv', 'notify'];

    function LoginCtrl($window, loginSrv, notify) {
        /* jshint validthis:true */
        var vm = this;
        vm.validateUser = function () {
             loginSrv.validateLogin(vm.username, vm.password).then(function (data) {          
                if (data.isValidUser) {    
                    $window.location.href = '/index.html';
                }
                else
                    alert('Login incorrect');
            });
        }
    }
})();
Run Code Online (Sandbox Code Playgroud)

  • @Junaid window.location.href用于传统的窗口对象,$ window.location.href用于AngularJS $窗口对象,在这里:https://docs.angularjs.org/api/ng/service/$window (12认同)
  • 不,但您可能需要向控制器注入$ window.看我编辑的答案. (3认同)
  • @ user3623224 - 实际上并非如此;) (3认同)
  • 它的window.location.href不是$ window.location.href (2认同)

Cri*_*anu 119

您可以通过不同方式重定向到新URL.

  1. 你可以使用$ window来刷新页面
  2. 您可以"留在"单页应用程序并使用$ location,在这种情况下您可以选择$location.path(YOUR_URL);$location.url(YOUR_URL);.因此,两种方法之间的基本区别在于,它$location.url()也会影响get参数$location.path().

我建议阅读文档$location,$window以便更好地掌握它们之间的差异.

  • 赞扬“ [RTFM](https://en.wikipedia.org/wiki/RTFM)”。 (2认同)

小智 14

$location.path('/configuration/streaming'); 这将工作...在控制器中注入位置服务


San*_*tra 12

我使用以下代码重定向到新页面

$window.location.href = '/foldername/page.html';
Run Code Online (Sandbox Code Playgroud)

并在我的控制器函数中注入$ window对象.


Ani*_*ngh 11

它可能会帮助你!!

AngularJs代码示例

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

app.config(function($stateProvider, $urlRouterProvider) {

  // For any unmatched url, send to /index
  $urlRouterProvider.otherwise("/login");

  $stateProvider
    .state('login', {
      url: "/login",
      templateUrl: "login.html",
      controller: "LoginCheckController"
    })
    .state('SuccessPage', {
      url: "/SuccessPage",
      templateUrl: "SuccessPage.html",
      //controller: "LoginCheckController"
    });
});

app.controller('LoginCheckController', ['$scope', '$location', LoginCheckController]);

function LoginCheckController($scope, $location) {

  $scope.users = [{
    UserName: 'chandra',
    Password: 'hello'
  }, {
    UserName: 'Harish',
    Password: 'hi'
  }, {
    UserName: 'Chinthu',
    Password: 'hi'
  }];

  $scope.LoginCheck = function() {
    $location.path("SuccessPage");
  };

  $scope.go = function(path) {
    $location.path("/SuccessPage");
  };
}
Run Code Online (Sandbox Code Playgroud)


Riz*_*izo 6

在 AngularJS 中,您可以使用如下方式将您的表单(提交时)重定向到其他页面window.location.href='';

postData(email){
    if (email=='undefined') {
      this.Utils.showToast('Invalid Email');
    } else {
      var origin = 'Dubai';
      this.download.postEmail(email, origin).then(data => { 
           ...
      });
      window.location.href = "https://www.thesoftdesign.com/";      
    }
  }
Run Code Online (Sandbox Code Playgroud)

简单地试试这个:

window.location.href = "https://www.thesoftdesign.com/"; 
Run Code Online (Sandbox Code Playgroud)


Vig*_*ian 5

我在 Angular 应用程序中重定向到不同页面时也遇到了问题

您可以添加$windowEwald 在他的回答中建议的,或者如果您不想添加$window,只需添加超时即可!

setTimeout(function () {
        window.location.href = "http://whereeveryouwant.com";
    }, 500);
Run Code Online (Sandbox Code Playgroud)