When is the $ asArray vs $ asObject method used in Angularfire?

There seem to be many situations where one could choose whether to use $asArray

or $asObject

in the same situation. You can get away from using it too.

I am starting a little with code / js, but when should I use $ asArray vs. $ asObject?

+3


source to share


2 answers


The latest version of angularfire 1.0.0

uses the new $ firebaseArray and $ firebaseObject services, which replace $ asArray and $ asObject when accessing collections or objects.

If you are using the latest version, you must convert $ asObject and $ asArray to new services.

var arr = $firebase(new Firebase(URL)).$asArray;
var obj = $firebase(new Firebase(URL)).$asObject;

      



becomes

var arr = $firebaseArray(new Firebase(URL));
var obj = $firebaseObject(new Firebase(URL));

      

More information is available at AngularFire Documentation

+4


source


You must use $asObject

for objects (hashes of key / value pairs) such a user profile, book description, or form data. Or, to quote the manual :

Objects are useful for storing key / value pairs and unique records that are not used as a collection.



You must use $asArray

for a collection, for example a list of users, books, or messages in a form.

In other words, if you iterate and use it in ng-repeat

, it is probably a collection. If you refer to properties by name, for example data.some_key

on it, it is probably an object.

+3


source







All Articles