如何在前端使用angular / angularfire列出来自firebase的用户名?

时间:2017-03-22 13:15:34

标签: angularjs firebase firebase-authentication angularfire

我正在尝试在列表中显示我的所有用户名的前端。

我正在创建这样的用户:

 firebase.initializeApp(config);

var app =  angular
.module('app', ['firebase'])
app.factory("Auth", ["$firebaseAuth",
function($firebaseAuth) {
return $firebaseAuth();
}
]);



 app.controller("SampleCtrl", ["$scope", "Auth",
 function($scope, Auth) {
$scope.createUser = function() {
  $scope.message = "One or more field is not correctly entered !";
  $scope.error = null;


Auth.$createUserWithEmailAndPassword($scope.email, $scope.password, $scope.name)
    .then(function(firebaseUser) {
      $scope.message = "User created with uid: " + firebaseUser.uid;
      const uid = firebaseUser.uid;
      const dbref = firebase.database().ref().child('users').child(uid);


      dbref.update({
        email: $scope.email,
        id: uid,
        name: $scope.name
      });
        console.log(dbref.uid);
    }).catch(function(error) {
      $scope.error = error;
    });
};




 $scope.deleteUser = function() {
  $scope.message = null;
  $scope.error = null;

  $scope.auth.$onAuthStateChanged(function(firebaseUser) {
  $scope.firebaseUser = firebaseUser;
});

  // Delete the currently signed-in user
  Auth.$deleteUser().then(function() {
    $scope.message = "User deleted";
  }).catch(function(error) {
    $scope.error = error;
  });
 };
 }
 ]);

用户在Firebase的Auth中注册,以及在数据库中注册 - 像魅力一样工作!

要在前端调用所有用户名,我会尝试以下操作:

  <ul><li ng-repeat='user in users'>{{users.name}}</li></ul>

没有任何成功。

有人知道如何实现这个目标吗?我的代码出了什么问题?

非常感谢!

1 个答案:

答案 0 :(得分:0)

使用此版本,您将获得firebase的所有注册用户

    var myapp = angular.module('sampleApp', ['firebase'])
    myapp.factory('usersList', function($firebaseArray) {
    //Get the firebase reference
    var ref = firebase.database().ref('users');
    //return the array of users
    return $firebaseArray(ref);
  });

  myapp.controller('SampleCtrl', function (usersList, $scope) {
    //Put the users array in the scope.users
    $scope.users = usersList;
  })

并为显示用户提供您使用的相同代码,并初始化您的firebase应用程序 我认为它可以帮到你

      <ul><li ng-repeat='user in users'>{{users.name}}</li></ul>