如何在angularJS中创建全局变量

amanuel2

我有这个问题,当您注册时会进入“用户”页面。并假设说“欢迎”,原因是我不确定该用户名是否出现在“网页上”……请帮助这里是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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在AngularJS中更新全局变量?

来自分类Dev

如何在Promise Angularjs中更改全局变量的值

来自分类Dev

如何在Swift中创建全局变量?

来自分类Dev

如何在android中创建全局变量?

来自分类Dev

如何在Elixir模块中创建全局变量

来自分类Dev

如何在C#中创建全局变量?

来自分类Dev

如何在axios中创建全局变量?

来自分类Dev

如何在PHP中动态创建全局变量

来自分类Dev

如何在单个控制器中创建全局变量并在Angularjs的工厂内部调用函数

来自分类常见问题

如何创建全局变量?

来自分类Dev

如何创建全局变量?

来自分类Dev

如何在angularjs应用程序中包含全局变量?

来自分类Dev

如何在golang html / template中创建全局变量并在多个位置进行更改?

来自分类Dev

如何在R中创建可在函数和sql语句中使用的全局变量?

来自分类Dev

如何在查询中创建全局变量并与JavaScript一起使用

来自分类Dev

如何在WPF中创建和使用全局变量(C#)

来自分类Dev

如何使用tkinter在嵌套函数中创建全局变量?

来自分类Dev

如何在C ++中具有全局变量

来自分类Dev

如何在php中取消设置全局变量?

来自分类Dev

如何在Cake Task的WithCriteria中利用全局变量?

来自分类Dev

如何在nunjucks中定义全局变量?

来自分类常见问题

如何在bash中的函数内修改全局变量?

来自分类Dev

如何在Python中访问全局变量?

来自分类Dev

python-如何在Flask中设置全局变量?

来自分类Dev

如何在点击事件中设置全局变量?

来自分类Dev

如何在python中实现C / C ++全局变量?

来自分类Dev

如何在codeIgniter中定义全局变量(值)

来自分类Dev

如何在r函数中访问全局变量

来自分类Dev

如何在Visual Basic中声明全局变量?

Related 相关文章

热门标签

归档