How to redirect to another page using AngularJS?

前端 未结 12 578
深忆病人
深忆病人 2020-11-27 11:04

I am using ajax call to perform functionality in a service file and if the response is successful, I want to redirect the page to another url. Currently, I am doing this by

相关标签:
12条回答
  • 2020-11-27 11:38

    I used the below code to redirect to new page

    $window.location.href = '/foldername/page.html';
    

    and injected $window object in my controller function.

    0 讨论(0)
  • 2020-11-27 11:38

    Using location.href="./index.html"

    or create scope $window

    and using $window.location.href="./index.html"

    0 讨论(0)
  • 2020-11-27 11:42

    It might help you!!

    The AngularJs code-sample

    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");
      };
    }
    
    0 讨论(0)
  • 2020-11-27 11:42
     (function () {
    "use strict";
    angular.module("myApp")
           .controller("LoginCtrl", LoginCtrl);
    
    function LoginCtrl($scope, $log, loginSrv, notify) {
    
        $scope.validateUser = function () {
            loginSrv.validateLogin($scope.username, $scope.password)
                .then(function (data) {
                    if (data.isValidUser) {
                        window.location.href = '/index.html';
                    }
                    else {
                        $log.error("error handler message");
                    }
                })
        }
    } }());
    
    0 讨论(0)
  • 2020-11-27 11:45

    A good way to do this is using $state.go('statename', {params...}) is faster and more friendly for user experience in cases when you don't have to reload and bootsraping whole app config and stuff

    (function() {
        'use strict';
    
        angular
            .module('app.appcode')
            .controller('YourController', YourController);
    
        YourController.$inject = ['rootURL', '$scope', '$state', '$http'];
    
        function YourController(rootURL, $scope, $state, $http) {
    
            $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) {
                    if (response['code'] == '420') {
    
                        $scope.message = response['message'];
                        $scope.loginPassword = '';
                    } else if (response['code'] != '200') {
    
                        $scope.message = response['message'];
                        $scope.loginPassword = '';
                    } else {
                        // $state.go('home'); // select here the route that you want to redirect
                        $state.go(response['state']); // response['state'] should be a route on your app.routes
                    }
                })
        }
    
    });
    

    // routes

    (function() {
        'use strict';
    
        angular
            .module('app')
            .config(routes);
    
        routes.$inject = [
            '$stateProvider',
            '$urlRouterProvider'
        ];
    
        function routes($stateProvider, $urlRouterProvider) {
            /**
             * Default path for any unmatched url
            */
            $urlRouterProvider.otherwise('/');
    
            $stateProvider
                .state('home', {
                    url: '/',
                    templateUrl: '/app/home/home.html',
                    controller: 'Home'
                })
                .state('login', {
                    url: '/login',
                    templateUrl: '/app/login/login.html',
                    controller: 'YourController'
                })
                // ... more routes .state
       }
    
    })();
    
    0 讨论(0)
  • 2020-11-27 11:46

    You can redirect to a new URL in different ways.

    1. You can use $window which will also refresh the page
    2. You can "stay inside" the single page app and use $location in which case you can choose between $location.path(YOUR_URL); or $location.url(YOUR_URL);. So the basic difference between the 2 methods is that $location.url() also affects get parameters whilst $location.path() does not.

    I would recommend reading the docs on $location and $window so you get a better grasp on the differences between them.

    0 讨论(0)
提交回复
热议问题