我已经使用 firebase 和 angularjs 创建了一个身份验证过程。
在身份验证过程之后,页面必须使用 $routeprovider
重定向到 htmlnew.html
。但它不会在身份验证后重定向。
index.html
<html ng-app="appName">
<body>
<div ng-controller="loginCtrl" id="message">
<a ng-click="googleSignin()">SignIn into App</a>
</div>
<p id="load">Firebase SDK Loading…</p>
</body>
</html>
htmlnew.html
<body>
<div id="message" ng-controller="loginpage">
<h1>Welcome</h1>
<a ng-click="googleSignout()">Signout</a>
</div>
</body>
app.js
var app = angular.module("appName", ["ngRoute", "firebase"]);
app.config(function ($routeProvider) {
$routeProvider
.when("/", {
templateUrl: 'public/index.html',
controller: 'loginCtrl'
})
.when('/login', {
templateUrl: 'public/htmlnew.html',
controller: 'loginpage'
})
});
app.controller('loginCtrl', function ($scope) {
var provider = new firebase.auth.GoogleAuthProvider();
$scope.googleSignin = function () {
firebase.auth()
.signInWithPopup(provider).then(function (result) {
var token = result.credential.accessToken;
var user = result.user;
console.log(token);
console.log(user);
}).catch(function (error) {
var errorCode = error.code;
var errorMessage = error.message;
console.log(errorCode);
console.log(errorMessage);
});
}
});
app.controller('loginpage', function ($scope) {
$scope.googleSignout = function () {
firebase.auth().signOut()
.then(function () {
console.log('Signout Succesfull');
}, function (error) {
console.log('Signout Failed');
});
}
});
项目文档
对于此项目的演示检查 link
提前致谢
最佳答案
使用 $location
重定向到路径。将其注入(inject) Controller 依赖项并按如下方式使用
firebase.auth()
.signInWithPopup(provider).then(function (result) {
$location.path('/login');
})
关于javascript - 页面没有在 Angularjs 中重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44409079/