When does one use $asArray vs $asObject in Angularfire?

后端 未结 2 1438
傲寒
傲寒 2021-01-20 23:49

It seems like there are many situations where one could choose whether to use $asArray or $asObject, in the same situation. You could get away with us

相关标签:
2条回答
  • 2021-01-21 00:03

    You should utilize $asObject for objects (hashes of key/value pairs), such a user's profile, a book description, or a form post's details. Or, to quote the guide:

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

    You should utilize $asArray for a collection, such as a list of users, books, or form posts.

    To put it another way, if you will iterate it and utilize it in ng-repeat, it's probably a collection. If you will access properties by name, like data.some_key on it, it's probably an object.

    0 讨论(0)
  • 2021-01-21 00:23

    The latest version of angularfire 1.0.0 uses new $firebaseArray and $firebaseObject services that replace $asArray and $asObject when accessing collections or objects.

    If you are using the latest version, you should convert $asObject and $asArray to the 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));
    

    Additional information is available at AngularFire Documentation

    0 讨论(0)
提交回复
热议问题