Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Redirect to a new page/url in AngularJS

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 24.4k
    Comment on it

    We use $location service or $window to redirect to a new URL. $location exposes the URL to the browsers address bar and makes it available to the application. It reflects the changes either side ,i.e. synchronizes the changes and maintains it between the browser’s URL and itself by keeping a watch in the URL.

    We should use $location whenever we want to change the current URL in the browser or there is a change in the current URL and your applications need to monitor or react to that change.

    With $location, there are two ways of using it :
    1. $location.path(URL);
    Syntax:

    $location.path('/newValue);

    2. $location.url(URL);
    Syntax:

    $location.url(/newValue);

    The $location does not do the full page reload. For reloading the page after changing the URL, we need to use the $window.location.href ( it will refresh the page ).
     Syntax:
     

    $window.location.href = 'http://abcd.com';

    An example code :-

    <body ng-app="myApp">
            <div ng-controller="myController">
                <div class="app">
                       <div id="deviceready">
                        <button class="button_start" ng-click = "Calling()"> Start </button>
                    </div>
                </div>
            </div>
            <script src="js/angular.min.js"></script>
            <script src="js/index.js"></script>
            <script src="js/myApp.js"></script>
        </body>


    In your controller :

    var myApp = angular.module('myApp', []);
    myApp.controller( "myController", function($scope) {
         var flag = 1;
         $scope.Calling = function(){
             console.log("enter mute calling);
             if(flag == 1){
                 $window.location.href = '/signin';
                 flag = 2;
             }else{
                 $window.location.href = /register;
                 flag = 1;
             }
         };
        
    });


    This code is just to show you the method of using $window.location.href. You need to modify it according to your application logic and code.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: