如何在指令中写入firebase

时间:2014-12-18 17:11:42

标签: angularjs firebase ionic-framework

我尝试从指令

向firebase写一些东西

这是我的指示[更新]:

angular.module('MyApp.directives').directive('score', function() {
    return {
      restrict: "E",
      scope: {
      },
      templateUrl: 'dashboard/score.html',
      controller: ['$scope', function ($scope) {

      }],
      link: function(scope, element, attrs, controller, firebase) {

          var eventsRef = new Firebase('https://URL.firebaseio.com/');
          scope.event_id = attrs.index;
          //Score counter
          scope.Score = [0, 0];
          scope.add_btn = function(num) {
            scope.Score[num]++;
          };
          scope.dist_btn = function(num) {    
            if (scope.Score[num] > 0) {
              scope.Score[num]--;
            } else {
              num = 0;
            }
          };

          scope.setPrediction = function () {
            eventsRef.child('/'+scope.event_id+'/').update(scope.Score);
          }
        }
      }
});

它的投掷错误:

  

FIREBASE INTERNAL ERROR:服务器错误:ClientId [7373865]:ErrorId [2]:传入消息出错

1 个答案:

答案 0 :(得分:1)

好的,我意识到wats发生了。 Firebase不喜欢发布没有键的数组

此代码正常运行:

angular.module('MyApp.directives').directive('score', function() {
    return {
      restrict: "E",
      scope: {
      },
      templateUrl: 'dashboard/score.html',
      controller: ['$scope', function ($scope) {

      }],
      link: function(scope, element, attrs, controller, firebase) {

          var eventsRef = new Firebase('https://sportsbettr2.firebaseio.com/events');
          scope.event_id = attrs.index;
          //Score counter
          scope.Score = [0, 0];
          scope.add_btn = function(num) {
            scope.Score[num]++;
          };
          scope.dist_btn = function(num) {    
            if (scope.Score[num] > 0) {
              scope.Score[num]--;
            } else {
              num = 0;
            }
          };

          scope.setPrediction = function () {
            eventsRef.child('/'+scope.event_id+'/').update({
                "score": scope.Score
            });
          }
        }
      }
});