如何在angularJS中创建全局变量

ama*_*el2 6 angularjs

我有这个问题,当你注册时,你进入用户页面.并且它假设说"欢迎"用户剂量剂出现在网页上因为我不确定...请帮助这里是plunkr:

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

        }, 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;
      });
    };*/
  }
]);
Run Code Online (Sandbox Code Playgroud)

P A*_*man 2

您的代码中有很多事情您可能需要处理,但有一些快速且部分肮脏的解决方案:

不要将所有 javascript 文件包含在嵌套模板中。路由到您的任何内容都ng-view应该只是您想要插入其中的 html <div>。没有 CSS 链接,没有脚本标签,只有通常位于页面正文中的 HTML。

在您的注册页面上,您的用户名ng-model需要与您作为参数传递给ng-click. 所以ng-model="userName"你不需要它来ng-model="username"匹配ng-click="createUser(username, email, password)"

您的另一个主要问题是$scope每次更改视图时都会被覆盖,因为每条路线都有相同的控制器。因此,从概念上讲,您可能会认为 the username(由于某种原因您以复数形式存储$scope.usernames)仍然可以在 上访问$scope。但事实并非如此,因为每个视图都在其自己唯一的身份验证控制器实例上运行。由于您不知道如何实现服务,最快的解决方案可能是注入$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;