我正在使用Firebase,Backend-as-a-Service,用于我的网站应用程序,由AngularJS编程.
我想使用db查询来获取记录,例如:WHERE name ==’The Legend Of Korra’
{ "series" : { "-JkqSvwyDw5F9DvGUu6b" : { "createdAt" : 1426842959884,"creators" : "Michael Dante DiMartino,Bryan Konietzko","description" : "The series follows the adventures of protagonist twelve-year-old Aang and his friends,who must bring peace and unity to the world by ending the Fire Lord's war against the other three nations.","episode_reset" : true,"genres" : "Action,Adventure,Fantasy,Comedy-drama","keywords" : "avatar,the last airbender","name" : "Avatar: The Last Airbender","official_website" : "http://nicktoons.nick.com/shows/avatar","publish_date" : "2005-02-21","updatedAt" : 1426843055127 } } }
我正在使用的代码使这个工作:
$scope.isSeriesValid = function(seriesName) { var seriesRef = new Firebase('http://<<firebaseapp>>.firebaseio.com/series'); var seriesCollection = $firebaseArray(seriesRef); seriesCollection.$loaded(function() { angular.forEach(seriesCollection,function(seriesObject,index) { if(seriesObject.name == seriesName) return true; }); }); return false; };
我想在不使用$firebaseArray和forEach的情况下完成这项工作.我该怎么做?
就像这样:
var seriesRef = new Firebase('http://<<firebaseapp>>.firebaseio.com/series'); var seriesObject = $firebaSEObject(seriesRef.query({ name: seriesName }));
我有一些建议可能会有所帮助:
原文链接:https://www.f2er.com/angularjs/141441.html>查看Firebase Query documentation.具体来说,
例
>看看这个工作PLNKR example.
>我在我的一个公共Firebase实例中复制了您的数据.
>您正在寻找的查询是seriesCollectionRef.orderByChild(“name”).equalTo(seriesName)
>如果您在输入中输入“Avatar:The Last Airbender”并单击“Find”,您将获得匹配的系列对象.
>在我的示例中,我扩展了$firebaseArray服务,以包含一个按名称查找特定系列的方法.
>请参阅extending AngularFire services的文档.
>您可以在不扩展服务的情况下完成相同的操作,请参阅上一个代码段.
工厂
app.factory('SeriesFactory',function(SeriesArrayFactory,fbUrl){ return function(){ var ref = new Firebase(fbUrl+'/series'); return new SeriesArrayFactory(ref); } }); app.factory('SeriesArrayFactory',function($firebaseArray,$q){ return $firebaseArray.$extend({ findSeries:function(seriesName){ var deferred = $q.defer(); // query by 'name' this.$ref().orderByChild("name").equalTo(seriesName).once("value",function(dataSnapshot){ if(dataSnapshot.exists()){ deferred.resolve(dataSnapshot.val()); } else { deferred.reject("Not found."); } }); return deferred.promise; } }); });
调节器
app.controller('HomeController',function($scope,SeriesFactory,fbUrl) { $scope.seriesName = ''; $scope.findSeries = function() { console.log("Finding series with name:'",$scope.seriesName,"'"); var seriesCollection = new SeriesFactory(); seriesCollection.findSeries($scope.seriesName).then(function(data){ console.log("Found",data); $scope.series = data; }).catch(function(error){ console.warn(error); }); }; });
没有扩展服务
如果您不使用工厂,这是控制器功能的样子:
$scope.findSeriesWithoutFactory = function() { var seriesRef = new Firebase(fbUrl+'/series'); var seriesCollection = $firebaseArray(seriesRef); seriesCollection.$ref().orderByChild("name").equalTo($scope.seriesName).once("value",function(dataSnapshot){ var series = dataSnapshot.val(); if(dataSnapshot.exists()){ console.log("Found",series); $scope.series = series; } else { console.warn("Not found."); } }); };
规则
注意:请务必注意,您应在Firebase规则中添加“.indexOn”:“name”,以便查询有效运行.像这样:
>请参阅Firebase Security & Rules Guide的Indexing Your Data部分.
"yourfirebaseapp": { ".read": "...",".write": "...","series": { ".indexOn": "name" } }
希望有所帮助!