angularjs – 使用查询检索记录,AngularFire

前端之家收集整理的这篇文章主要介绍了angularjs – 使用查询检索记录,AngularFire前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用Firebase,Backend-as-a-Service,用于我的网站应用程序,由AngularJS编程.

我想使用db查询获取记录,例如:WHERE name ==’The Legend Of Korra’

我在Firebase服务器中有一系列电视剧,JSON代码

{
  "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 }));
我有一些建议可能会有所帮助:

>查看Firebase Query documentation.具体来说,

> .orderByChild()
> .equalTo()

>您可以将查询.$ref()结合使用以获取所需的记录.

>看看这个工作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 GuideIndexing Your Data部分.

"yourfirebaseapp": {
    ".read": "...",".write": "...","series": {
      ".indexOn": "name"
    }
  }

希望有所帮助!

原文链接:https://www.f2er.com/angularjs/141441.html

猜你在找的Angularjs相关文章