当前位置: 动力学知识库 > 问答 > 编程问答 >

angularjs - 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!

网友答案:

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

网友答案:

should have:

$location.path('/homepage'); 

Or:

$location.path('/app/homepage');
分享给朋友:
您可能感兴趣的文章:
随机阅读: