Angularjs 应用程序移动到firebase时未加载数据
我有一个应用程序,我有一些下拉列表,用于过滤数据。一旦我将json移动到firebase,我就无法检索这些下拉列表中的数据。 以下是控制台错误:Angularjs 应用程序移动到firebase时未加载数据,angularjs,firebase,Angularjs,Firebase,我有一个应用程序,我有一些下拉列表,用于过滤数据。一旦我将json移动到firebase,我就无法检索这些下拉列表中的数据。 以下是控制台错误: TypeError: Cannot read property 'tags' of undefined at classifieds.ctr.js:135 at Object.forEach (angular.js:321) at getTags (classifieds.ctr.js:134) at classifieds.ctr.js:20 at
TypeError: Cannot read property 'tags' of undefined
at classifieds.ctr.js:135
at Object.forEach (angular.js:321)
at getTags (classifieds.ctr.js:134)
at classifieds.ctr.js:20
at processQueue (angular.js:15552)
at angular.js:15568
at Scope.$eval (angular.js:16820)
at Scope.$digest (angular.js:16636)
at angular.js:16859
at completeOutstandingRequest (angular.js:5804)
以下是代码:
angular
.module("ngClassifieds")
.controller("classifiedsCtrl", function($scope, $state, $http, classifiedsFactory, $mdSidenav, $mdToast, $mdDialog) {
$scope.classifieds = classifiedsFactory.ref;
$scope.classifieds.$loaded().then(function(classifieds) {
$scope.tags = getTags(classifieds); // call the getTags method below
$scope.books = getBooks(classifieds); // call the getBooks method below
$scope.authors = getAuthors(classifieds); // call the getAuthors method below
$scope.order = ""; //for sorting in asc or desc order
});
下面是getTags方法:
function getTags(classifieds) {
var tags = [];
angular.forEach(classifieds, function(item) {
angular.forEach(item.meta.tags, function(tag) {
tags.push(tag);
});
});
return _.uniq(tags);
}
以下是firefox中的错误:
return语句后的不可访问代码
Error: item.meta is undefined
getTags/<@http://localhost:8080/components/classifieds/classifieds.ctr.js:135:5
forEach@http://localhost:8080/node_modules/angular/angular.js:321:11
getTags@http://localhost:8080/components/classifieds/classifieds.ctr.js:134:4
@http://localhost:8080/components/classifieds/classifieds.ctr.js:20:18
processQueue@http://localhost:8080/node_modules/angular/angular.js:15552:28
scheduleProcessQueue/<@http://localhost:8080/node_modules/angular/angular.js:15568:27
$RootScopeProvider/this.$get</Scope.prototype.$eval@http://localhost:8080/node_modules/angular/angular.js:16820:16
$RootScopeProvider/this.$get</Scope.prototype.$digest@http://localhost:8080/node_modules/angular/angular.js:16636:15
$RootScopeProvider/this.$get</Scope.prototype.$evalAsync/<@http://localhost:8080/node_modules/angular/angular.js:16859:15
completeOutstandingRequest@http://localhost:8080/node_modules/angular/angular.js:5804:7
Browser/self.defer/timeoutId<@http://localhost:8080/node_modules/angular/angular.js:6081:7
错误:item.meta未定义
getTags/AngularFire$loaded()
事件适用于非常有限的特定用例集,您希望以不同于其他数据的方式处理最初加载的数据。这里不是这种情况,因此您不应该使用$loaded()
如果数据库中有要在视图中显示的标记列表,则应将该标记列表放入$firebaseArray()
:
这将加载标记,并在加载后自动更新视图。更好的是,它还将监视数据库的更改,并在数据更改时更新视图
对$scope.books
和$scope.authors
执行同样的操作,您将有一个更轻松的时间。设置三个$firebaseArray()
对象,而不是一个$firebaseObject()
,可能会让人感觉不太直观。但是无论哪种方法,成本都差不多。Frank,但是如果要实现这个建议,我该如何调用getTags()方法呢?这里的coz tags指的是一个新定义的数组,它只存储json中每个对象/单词上的“tags”属性的所有标记的一个实例,所以您尝试过滤数据。对于$loaded()
,这也是可能的,但仍然不是一个好的用例。您需要扩展$firebaseArray()
类。可能类似于加藤(AngularFire的主要作者)的回答:谢谢弗兰克,我没能看透你提到的帖子中的所有建议,但我就是这样实现的,现在所有的数据都很好<代码>函数getTags(classifiedds){var tags=[];angular.forEach(classifieds,函数(item){if(item.meta){angular.forEach(item.meta.tags,函数(tag){tags.push(tag);}}});返回{uniq(tags)}
请参阅和
var tagsRef = ref.child('tags');
$scope.tags = $firebaseArray(tagsRef);