无法在控制器中使用 AngularFire 从 $firebaseObject 获取我的数据。 View/ng-repeat 工作正常

Unable to get my data from $firebaseObject using AngularFire in a controller. View/ng-repeat works fine

我在 Firebase 中有不同的部分包含规范化数据,我有获取信息的例程,但我无法循环遍历 returned 记录来获取数据。我想使用 $firebaseArray() 中的键从其他 $firebaseObject() 获取数据。

GetOneTeam() .... {
    var DataRef = GetFireBaseObject.DataURL(Node + '/');  // xxx.firebaseio.com/Schedules/
    var OneRecordRef = DataRef.child(Key); // Schedule Key - 1
    return $firebaseObject(OneRecordRef);
}

...

var Sched = GetOneSchedule('Schedules', 1);
...   // For Loop getting data - Put in HomeId
var TeamRec = GetOneTeam('Teams', HomeId);
var Name = TeamRec.TeamName;    // Does not TeamName value from Schedule/1

以下是更多实际代码,以防上面的代码片段不够清晰。获取数据的示例常用例程:

angular.module('MyApp')
    .constant('FIREBASE_URL', 'https://xxxxxxxx.firebaseio.com/');

angular.module('MyApp')
    .factory('GetFireBaseObject', function(FIREBASE_URL) {
        return {
            BaseURL: function() {
                return new Firebase(FIREBASE_URL);
            },
            DataURL: function(Node) {
                return new Firebase(FIREBASE_URL + Node);
            }
        };
    }
);

// Common code for getting Array/Object from Firebase.  
angular.module('MyApp')
    .factory("FireBaseData", ["$firebaseArray", "$firebaseObject", "GetFireBaseObject", 
    function($firebaseArray, $firebaseObject, GetFireBaseObject) {
        return {
            AllRecords: function(Node) {
                var DataRef = GetFireBaseObject.DataURL(Node + '/');
                return $firebaseArray(DataRef);
            },
            OneRecordAllChildren: function(Node, Key) {
                var DataRef = GetFireBaseObject.DataURL(Node + '/');
                var ParentRecordRef = DataRef.child(Key);
                return $firebaseArray(ParentRecordRef);
            },
            OneRecord: function(Node, Key) {
                var DataRef = GetFireBaseObject.DataURL(Node + '/');
                var OneRecordRef = DataRef.child(Key);
                return $firebaseObject(OneRecordRef);
            },
            AddRecord: function(Node, Record) {
                var DataRef = GetFireBaseObject.DataURL(Node + '/');
                var AddRecordRef = DataRef.child(Record.Key);               
                AddRecordRef.update(Record);
                return $firebaseObject(AddRecordRef);           // Return Reference to added Record
            },
            DeleteRecord: function(Node, Key) {
                var DataRef = GetFireBaseObject.DataURL(Node + '/');
                var DeleteRecordRef = DataRef.child(Key);
                DeleteRecordRef.remove();
            }
        };
    }
]);

个人控制者从 firebase.io 检索记录:

angular.module('MyApp').service("ScheduleData", ["FireBaseData",
    function(FireBaseData) {
        var DataPath = 'Schedules';

        this.AllSchedules = function() {
            return FireBaseData.AllRecords(DataPath);
        };

        this.AddSchedule = function(GameInfo) {
            return FireBaseData.AddRecord(DataPath, GameInfo);
        };

        this.DeleteSchedule = function(GameKey) {
            FireBaseData.DeleteRecord(DataPath, GameKey);
        };      

        this.GetOneSchedule = function(GameKey) {
            return FireBaseData.OneRecord(DataPath, GameKey);
        };
    }
]);

// Structure of a record, including named fields to come from another object (Team/Venue using the OneRecord FireBaseData call to get a $firebaseObject
angular.module('MyApp').factory("ScheduleRecord", function() {
        return {
            Clear: function(GameInfo) {
                GameInfo.Key = "";
                GameInfo.HomeTeamId = "";
                GameInfo.HomeTeamName = "";
                GameInfo.AwayTeamId = "";
                GameInfo.AwayTeamName = "";
                GameInfo.VenueId = "";
                GameInfo.VenueName = "";
                GameInfo.GameDate = "";
                GameInfo.GameTime = "";
            }
        };
    }
);

控制器模块启动:

angular.module('MyApp').controller('ScheduleCtrl', ["$scope", "ScheduleData", "ScheduleRecord", "TeamData", "VenueData",
    function ($scope, ScheduleData, ScheduleRecord, TeamData, VenueData) {

        var ClearEditData = function() {
            $scope.ScheduleEditMode = false;
            ScheduleRecord.Clear($scope.schedule);
        };

        var GameSchedules = ScheduleData.AllSchedules();

下一篇就是我的问题所在。一旦承诺 return 是静态时间表列表,我想遍历每条记录并将团队 ID (Home/Away) 和场地 ID 转换为名称。

        GameSchedules.$loaded().then(function() {
            angular.forEach(GameSchedules, function(GameInfo) {
                var HomeTeam = TeamData.GetOneTeam(GameInfo.HomeTeamId);
                GameInfo.HomeTeamName = HomeTeam.Name;

GetOneTeam return 是一个 $firebaseObject,基于 HomeTeamId 子记录。这 return 一直都是 null。

这也是使用 FireBaseData 的 TeamData.GetOneTeam return。

angular.module('MyApp').service("TeamData", ["FireBaseData",
    function(FireBaseData) {
        var DataPath = 'Teams';

        this.AllTeams = function() {
            return FireBaseData.AllRecords(DataPath);
        };

        this.AddTeam = function(TeamInfo) {
            return FireBaseData.AddRecord(DataPath, TeamInfo);
        };

        this.DeleteTeam = function(TeamKey) {
            FireBaseData.DeleteRecord(DataPath, TeamKey);
        };      

        this.GetOneTeam = function(TeamKey) {
            return FireBaseData.OneRecord(DataPath, TeamKey);
        };
    }
]);

因为我有一个 Firebase 对象,我如何从 $firebaseObject 中获取我的命名数据对象?

这是一团糟。将 $firebaseArray 用于集合,而不是 $firebaseObject。大多数这些奇怪的包装工厂都是不必要的。 AngularFire 服务已经有了添加、删除等方法,所有这些工厂都试图将 AngularFire 变成一个 CRUD 模型,实际上并不提供任何额外的功能或增强。

app.factory('Ref', function(FIREBASE_URL) {
   return new Firebase(FIREBASE_URL);
});

app.factory('Schedules', function($firebaseArray, Ref) {
   return $firebaseArray(Ref.child('Schedules'));
});

// or if you want to pass in the path to the data...
//app.factory('Schedules', function($firebaseArray, Ref) {
//   return function(pathToData) {
//      return $firebaseArray(Ref.child(pathToData));
//   };
//});

app.factory('Schedule', function($firebaseObject, Ref) {
  return function(scheduleId) {
     return $firebaseObject(Ref.child('Schedules').child(scheduleId));
  }
});

app.controller('...', function(Schedules, Schedule, Ref) {

  $scope.newSchedule(data) {
    Schedules.$add(data);
  };

  $scope.removeSchedule(key) {
    Schedules.$remove(key);
  };

  $scope.updateSchedule(key, newWidgetValue) {
     var rec = Schedules.$getRecord(key);
     rec.widgetValue = newWidgetValue;
     Schedules.$save(rec);
  };

  // get one schedule
  var sched = Schedule(key);
  sched.$loaded(function() {
     sched.widgetValue = 123;
     sched.$save();
  });


});