Wallcraft
Wallcraft

Reputation: 21

Redirect to another page with $location.url wont work

I'm new to AngularJS and i want to redirect my Application from Login.html to Homepage.html. I've read a lot and i found two ways for do that:

the first one consists in using $window.location.hrefand it works perfectly

the second one consist in using $location.url or $location.pathand it doesn't work, it ad just /homepage.html to my actual URL.

How can i solve this? Here's my code.

<!DOCTYPE html>
<html>
<head>
    <title>Login</title>
    <script src= "https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
</head>
<body>
    <div ng-app="mainApp" ng-controller="loginController">
        <label>Username</label><input type="text" name="username" ng-model="username"><br>
        <label>Password</label><input type="password" name="password" ng-model="password"><br>
        <button ng-click="login()">Login</button>
        <p>{{result}}</p>
    </div>
    <script>
        var mainApp = angular.module("mainApp", []);

        mainApp.controller('loginController', ['$scope', '$location', function($scope, $location){
            $scope.result = null;
            $scope.login = function(){
                $scope.result = "Logged in";
                $location.path('#/localhost/homepage.html').replace();
                $scope.apply();
            }
        }])
    </script>

</body>
</html>

Thanks you all anticipatedly!

Upvotes: 0

Views: 120

Answers (2)

e7lT2P
e7lT2P

Reputation: 1921

Υου should have:

$location.path('/homepage'); 

Or:

$location.path('/app/homepage');

Upvotes: 0

Dr. Cool
Dr. Cool

Reputation: 3721

Not sure if it's important or not, but try changing $scope.apply() to $scope.$apply().

Upvotes: 1

Related Questions