Foreach with firebase

时间:2015-06-30 10:39:58

标签: angularjs for-loop foreach firebase angularfire

var myApp = angular.module("myApp", ["firebase"]);
var nick = prompt("Anna nimesi");


myApp.controller("MyController", ["$scope", "$firebaseArray",
  function($scope, $firebaseArray) {
    var msgref = new Firebase("https://(myapp).firebaseio.com/messages");
    var usrref = new Firebase("https://(myapp).firebaseio.com/users");

    $scope.messages = $firebaseArray(msgref);
    $scope.users = $firebaseArray(usrref);

    console.log($scope.users);
    console.log($scope.messages);


    var taken = false;
    angular.forEach($scope.users, function(value, key) {
      console.log(value, key);

      if (value.username == nick) {
        taken = true;
      }


      // put your code here
    });


    if (taken == false) {
      $scope.users.$add({
        username: nick
      });
    };







    $scope.addMessage = function(e) {
      if (e.keyCode == 13 && $scope.msg) {
        var name = nick || "anynomous";


        $scope.messages.$add({
          from: name,
          body: $scope.msg
        });

        $scope.msg = "";

      }

    }


  }
])
<!DOCTYPE html>
<html ng-app="myApp">

<head>
  <link rel="stylesheet" type="text/css" href="style.css">
  <meta charset="utf-8" />
  <title>title</title>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
  <script src="https://cdn.firebase.com/js/client/2.2.4/firebase.js"></script>
  <script src="https://cdn.firebase.com/libs/angularfire/1.1.1/angularfire.min.js"></script>

</head>

<body ng-controller="MyController">
  <div class="example-chat l-demo-container">
    <ul id="example-messages" class="example-chat-messages">
      <li ng-repeat="msg in messages">
        <strong class="example-chat-username">{{msg.from}}</strong>
        {{msg.body}}
      </li>
    </ul>

  </div>
  <input class="input" autofocus="true" ng-model="msg" ng-keydown="addMessage($event)" type="text" id="messageInput" placeholder="kirjota">

  <script src="script.js"></script>
</body>

</html>

我正在聊天,我想检查是否已经有一个同名的用户,这样就不会有两个用户有相同的昵称。这不起作用,我可以添加许多相同的刻痕。

var usrref = new Firebase("https://(myapp).firebaseio.com/users");
$scope.users = $firebaseArray(usrref);

var taken = false;
for (var usr in $scope.users) {
if(usr.username == nick){
    taken = true;
};};

if(taken == false){
    $scope.users.$add({username:nick});
    };

我的消息在html中与ng-repeat完美展示,但我无法完成这项工作。它显然很简单,但我已经用这个挣扎了太长时间。

2 个答案:

答案 0 :(得分:1)

由于你使用的是Angular,我建议使用Angular的forEach method。以下是您可以做的一个示例:

public static class StringExtension
{
    public static string ReplaceNewLine(this string s, string c = "|")
    {
        return s.Replace(c, Environment.NewLine);
    }
}

<强>被修改

这对你有用。它会在从Firebase加载数据后检查您的数据。如果用户名存在,则不会添加名称。如果它不存在,则添加它。您可以查看$loaded()的AngularFire API。

angular.forEach($scope.users, function(value, key) {
   // put your code here
});

答案 1 :(得分:0)

for (var ... in ...)迭代对象键。因此,如果$scope.users是一个数组,您将获得0, 1, 2, ...变量中的索引:usr。要遍历数组,您可以使用以下构造:

$scope.users.forEach(function(user) {

});

但也许不使用数组,最好使用用户名索引的对象。如果给定用户存在(users[nick] !== undefined),您将获得即时信息,并且您还可以避免其他一些麻烦。请参阅Best Practices: Arrays in Firebase