我有这个问题,当您注册时会进入“用户”页面。并假设说“欢迎”,原因是我不确定该用户名是否出现在“网页上”……请帮助这里是plunkr:
http://plnkr.co/edit/qB3Gkeq5ji1YQyy0kpGH?p=预览
请我需要帮助。
我需要为punker获取一些代码,例如:script.js:
var app = angular.module('LoginApp', ["firebase", "ngRoute"])
app.config(function ($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'registration.html',
controller: 'AuthCtrl'
})
.when('/logIn', {
templateUrl: 'login.html',
controller: 'AuthCtrl'
})
.when('/User', {
templateUrl: "User.html",
controller: 'AuthCtrl'
})
.otherwise({
redirectTo: '/'
});
});
app.factory("Auth", ["$firebaseAuth",
function($firebaseAuth) {
var ref = new Firebase("https://uniquecoders.firebaseio.com/");
return $firebaseAuth(ref);
}
]);
app.controller("AuthCtrl", ["$scope", "Auth",
function($scope, Auth) {
$scope.createUser = function() {
$scope.message = null;
$scope.error = null;
var ref2 = new Firebase("https://uniquecoders.firebaseio.com/");
ref2.createUser({
email: $scope.email,
password: $scope.password
}, function(error, userData) {
if (error) {
switch (error.code) {
case "EMAIL_TAKEN":
alert("The new user account cannot be created because the email is already in use. Try to login");
break;
case "INVALID_EMAIL":
alert("The specified email is not a valid email.");
break;
case "INVALID_PASSWORD":
alert("The Specified Passowrd Is not valid.")
break;
default:
alert("Error creating user:", error);
}
} else {
alert("Successfully created user account with uid:", userData.uid);
alert($scope.UserName)
window.location.hash = "/User"
$scope.usernames = "HEY"
}
});
};
$scope.logIn = function(){
$scope.message = null;
$scope.error = null;
ref2.authWithPassword({
"email" : $scope.logInemail,
"password" : $scope.logInemailpassword
}, function(error, userData){
if(error){
alert("Login Failed.")
console.log(error)
}
else{
alert("Logged In!")
}
})
}
/* $scope.removeUser = function() {
$scope.message = null;
$scope.error = null;
Auth.$removeUser({
email: $scope.email,
password: $scope.password
}).then(function() {
$scope.message = "User removed";
}).catch(function(error) {
$scope.error = error;
});
};*/
}
]);
您的代码中可能要处理很多事情,但是有一些快速且部分肮脏的解决方案:
不要在嵌套模板中包含所有JavaScript文件。路由到您的任何内容都ng-view
应该是您要在其中插入的html <div>
。没有CSS链接,没有脚本标签,只有HTML,通常位于页面正文中。
在您的注册页面上,您的用户名ng-model
需要与您作为参数传递的内容相匹配ng-click
。因此,ng-model="userName"
您不需要ng-model="username"
匹配它ng-click="createUser(username, email, password)"
。
您的另一个主要问题是,$scope
每次更改视图时都会被覆盖,因为每条路由都具有相同的控制器。因此,从概念上讲,您可能会认为username
($scope.usernames
出于某种原因将其存储为复数)仍然可以在上访问$scope
。但事实并非如此,因为每个视图都在其自己的Auth Controller唯一实例上运行。因为您不知道如何实现服务,所以最快的解决方案可能是注入$rootScope
到控制器中,然后usernames
使用$rootScope
而不是$scope
(尽管记住$rootScope
在生产环境中使用是不正确的做法),因为$ rootScope将在您的所有对象中持续存在控制器。因此,您的javascript文件看起来像这样:
app.controller("AuthCtrl", ["$scope", "$rootScope", "Auth", function($scope, $rootScope, Auth) {
接着
$scope.createUser = function(username, email, password) { $rootScope.usernames = username $scope.message = null; $scope.error = null;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句