javascript - Return value from firebase in ng-repeat -
i use angularjs front-end , firebase database.
i have ng-repeat
displays firebasearray
of stories. each of these stories connected number of collections can part of. collections , stories stored in 2 different locations on firebase server , use keys tie these together.
my model story object in database looks this:
storyid: { title: 'title', in_collections: { 0: collectionid //collection id refers specific collection in database 1: collectionid } }
so whenever display story object don't have access title, id. in order title need call server name. have this:
template:
<div> {{story.title}} in collections: <span ng-repeat="(key, collection) in story.in_collections"> {{getcollectiontitle(key)}} <span> </div>
controller:
app.controller("explorectrl", function($scope, stories, collections){ $scope.story = stories.getstory(storyid); //comes firebase array $scope.getcollectiontitle = function(collectionid){ collections.getcollectiontitle(collectionid).then(function(title){ console.log(title.val()); return title.val(); }); } });
services:
app.factory('collections', function(){ var ref = firebase.database().ref('collections'); return { getcollectiontitle: function(collectionid){ return ref.child(collectionid).child('title').once('value'); } } }) .factory('stories', function($firebaseobject){ var ref = firebase.database().ref('stories'); return{ getstory: function(id){ return $firebaseobject(ref.child(id)); } } })
i can see fetches correct collection title in console.log(title.val())
can't find way apply view. empty , keeps being way.
any appreciated. thank much!
Comments
Post a Comment