如何使用功能之间的变量 [英] How to use variable between functions

查看:134
本文介绍了如何使用功能之间的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用功能之间的变量的问题。正如你所看到楼下User.username可用且擅长的注册网页,但是当你去到登录页面我告诉它第一次警报User.username的价值,它提醒未定义?我很困惑在这里。我是pretty确保我在这里缺少一个概念。不管怎么说太感谢你了!

下面是一个plunkr: http://plnkr.co/edit/qB3Gkeq5ji1YQyy0kpGH

下面是我的的script.js 的:

  app.controller(AuthCtrl,[$范围,验证,$ rootScope
  功能($范围,验证,$ rootScope){
变种用户= {}      $ scope.createUser =功能(用户名,电子邮件,密码){
        $ rootScope.usernames =用户名
        User.username =用户名
        $ scope.message = NULL;
        $ scope.error = NULL;
    VAR REF2 =新的火力地堡(HTTPS://unique$c$crs.firebaseio.com/);
  ref2.createUser({
    电子邮件:$ scope.email,
    密码:$ scope.password
  },功能(错误,用户数据){
    如果(错误){
      开关(误差。code){
        案EMAIL_TAKEN:
          警报(不能创建新的用户帐户,因为电子邮件是已在使用尝试登录。);
          打破;
        案INVALID_EMAIL:
          警报(指定的电子邮件是不是有效的电子邮件。);
          打破;
        案INVALID_PASSWORD:
          警报(以下简称指定Passowrd是无效的。)
          打破;
        默认:
          警报(创建用户时出错:错误);
      }
    }其他{
      警报(+ User.username使用用户名成功创建用户帐户);      window.location.hash =/用户
    }
  });
      };       $ scope.logIn =功能(){
         警报(User.username)
       $ rootScope.usernames = User.username
        $ scope.message = NULL;
        $ scope.error = NULL;
        VAR REF2 =新的火力地堡(HTTPS://unique$c$crs.firebaseio.com/);
        ref2.authWithPassword({
          电子邮件:$ scope.logInemail,
          密码:$ scope.logInemailpassword        },功能(错误,用户数据){          如果(错误){
            警报(登录失败,因为:+误差)
          }
          其他{
            警报(登录!)
            window.location.hash =/用户
          }        })      }  / * $ scope.removeUser =功能(){
      $ scope.message = NULL;
      $ scope.error = NULL;      验证。$ removeUser({
        电子邮件:$ scope.email,
        密码:$ scope.password
      })。然后(函数(){
        $ scope.message =用户删除;
      })赶上(功能(错误){
        $ scope.error =错误;
      });
    }; * /
  }
]);


解决方案

在页面重新加载重新执行整个的JavaScript。在人口登记过程中所以用户对象不再登录功能可用,因为页面上点击注册按钮重新加载。结果
因此,我们需要的用户名存储在cookie的,这样我们就可以访问它的任何时间。即使当它从JavaScript运行中删除它会在那里浏览器缓存中。结果
它的风险在饼干密码存储的安全问题。结果

在注册成功保存在cookie中的用户名。结果
一些这样的事

 的document.cookie =用户名=+用户名+,+电子邮件=+电子邮件;

在登录功能得到cookie的值,并检查作为进入如果用户的电子邮件是一样的。

 函数的getCookie(名称){
   变种成对= document.cookie.split(;),
   数= pairs.length,部分;
   而(count--){
    部分=双[统计] .split(=);
    如果(部件[0] ===名)
        回部分[1];
}
 返回false;
}
VAR用户名=的getCookie(用户名);
VAR电子邮件=的getCookie(用户名);
如果($ scope.logInemail ==电子邮件)
  {
     警报(用户名);
  }

I'm having a problem of using variables between functions. As you can see down below User.username is available and good at the sign up page, but when you go to the login page I told it to first alert the value of User.username, and it alerts undefined? I'm confused here. I'm pretty sure I'm missing a concept here. Anyways Thank you so much!:

Here is a plunkr: http://plnkr.co/edit/qB3Gkeq5ji1YQyy0kpGH

Here is my script.js:

app.controller("AuthCtrl", ["$scope", "Auth","$rootScope", 
  function($scope, Auth, $rootScope) {
var User = {}

      $scope.createUser = function(username, email, password) {
        $rootScope.usernames = username
        User.username = username
        $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 username" + User.username);

      window.location.hash = "/User"
    }
  });


      };

       $scope.logIn = function(){
         alert(User.username)
       $rootScope.usernames = User.username
        $scope.message = null;
        $scope.error = null;
        var ref2 = new Firebase("https://uniquecoders.firebaseio.com/");
        ref2.authWithPassword({
          "email" : $scope.logInemail,
          "password" : $scope.logInemailpassword

        }, function(error, userData){

          if(error){
            alert("Login Failed Because : " + error)
          }
          else{
            alert("Logged In!")
            window.location.hash = "/User"
          }

        })

      }

  /*  $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;
      });
    };*/
  }
]);

解决方案

When page reloads the entire javascript is re executed. So user object populated in registration process is no longer available in login function because page has reloaded on clicking on signup button.
So we need to store the user name in cookie so that we can access it any time. Even when its removed from javascript runtime it will be there in the browser cache.
Its risky to store passwords in cookies for security issues.

On successful registration save the username in cookie.
Some thing like this

document.cookie ="username =" +username + "; " +"email ="+ email;

In login function get cookie values and check if email of user is same as entered

function getCookie(name ) {
   var pairs = document.cookie.split("; "),
   count = pairs.length, parts; 
   while ( count-- ) {
    parts = pairs[count].split("=");
    if ( parts[0] === name )
        return parts[1];
}
 return false;
}


var username=getCookie("username");
var email=getCookie("username");
if($scope.logInemail ==email)
  {
     alert(username);
  }

这篇关于如何使用功能之间的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆