http://plnkr.co/edit/qB3Gkeq5ji1YQyy0kpGH?p=preview
我需要帮助..
我需要为plunker获取一些代码,所以:
的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 },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视图中路由到的任何内容都应该是您要在< div>中插入的html.没有CSS链接,没有脚本标签,只有通常位于页面正文内的HTML.
在您的注册页面上,您的用户名ng-model需要与您作为ng-click参数传递的内容相匹配.因此,不需要ng-model =“userName”,而是需要ng-model =“username”来匹配ng-click =“createUser(用户名,电子邮件,密码)”.
您的另一个主要问题是每次更改视图时都会覆盖$scope,因为每个路由都有相同的控制器.因此,从概念上讲,您可能认为用户名(由于某种原因存储为复数$scope.usernames)可供您在$scope上访问.但事实并非如此,因为每个视图都在其自己的Auth Controller实例上运行.您不知道如何实现服务的最快解决方案可能是将$rootScope注入您的控制器,然后将用户名放在$rootScope而不是$scope上(尽管请记住在生产中使用$rootScope被认为是不好的做法),因为$rootScope将在所有控制器中保持不变.所以你的javascript文件看起来更像是这样的:
app.controller(“AuthCtrl”,[“$scope”,“$rootScope”,“Auth”,
function($scope,$rootScope,Auth){
然后
$scope.createUser = function(用户名,密码){$rootScope.usernames = username$scope.message = null;$scope.error = null;