How to redirect to another page using AngularJS?

前端 未结 12 579
深忆病人
深忆病人 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:46

    $location.path('/configuration/streaming'); this will work... inject the location service in controller

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

    The simple way I use is

    app.controller("Back2Square1Controller", function($scope, $location) {
        window.location.assign(basePath + "/index.html");
    });
    
    0 讨论(0)
  • 2020-11-27 11:57

    If you want to use a link then: in the html have:

    <button type="button" id="btnOpenLine" class="btn btn-default btn-sm" ng-click="orderMaster.openLineItems()">Order Line Items</button>
    

    in the typescript file

    public openLineItems() {
    if (this.$stateParams.id == 0) {
        this.Flash.create('warning', "Need to save order!", 3000);
        return
    }
    this.$window.open('#/orderLineitems/' + this.$stateParams.id);
    

    }

    I hope you see this example helpful as it was for me along with the other answers.

    0 讨论(0)
  • 2020-11-27 12:01

    I faced issues in redirecting to a different page in an angular app as well

    You can add the $window as Ewald has suggested in his answer, or if you don't want to add the $window, just add an timeout and it will work!

    setTimeout(function () {
            window.location.href = "http://whereeveryouwant.com";
        }, 500);
    
    0 讨论(0)
  • 2020-11-27 12:02

    In AngularJS you can redirect your form (on submit) to other page by using window.location.href=''; like below:

    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/";      
        }
      }
    

    Simply try this:

    window.location.href = "https://www.thesoftdesign.com/"; 
    
    0 讨论(0)
  • 2020-11-27 12:03

    You can use Angular $window:

    $window.location.href = '/index.html';
    

    Example usage in a contoller:

    (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');
                });
            }
        }
    })();
    
    0 讨论(0)
提交回复
热议问题