2016-05-09 9 views
1

現在、私はhtmlで画像を受け取るようにハードコードされたURLの配列を持っています。Json Arrayにfirebaseレスポンスを格納する方法

$scope.product = { 
    "images" : [ 
    {"image": "https://s3-us-west-2.amazonaws.com/s.cdpn.io/194929/colorburst-700x700.jpg"}, 
    {"image": "https://s3-us-west-2.amazonaws.com/s.cdpn.io/194929/colorburst-700x700.jpg"}, 
    {"image": "https://s3-us-west-2.amazonaws.com/s.cdpn.io/194929/colorburst-700x700.jpg"} 

    ] }; 

私は、データベースからデータを受け取る方法について学習しましたが、私は、私は現在受け付けております「$のscope.product」

に示されているもののようなJSON配列に結果を保存しますか

// Get a reference to our posts 
var ref = new Firebase("https://<database>.firebaseio.com/profile"); 
// Retrieve new posts as they are added to our database 
ref.on("child_added", function(snapshot, prevChildKey) { 
    var newPost = snapshot.val(); 
    debugger; 
    console.log("The email is: " + newPost.email); 
}); 

答えて

1

を介したデータには、JavaScriptの配列やオブジェクトは、彼らが一致どのように密接にもかかわらず、JSONの配列や辞書ではないことに注意してください。それはあなたがそれを受け取ると、データからJavaScriptオブジェクトを作成するように見えます。あなたのコードを考える

、このような何かにそれを調整することについて、あなたは求めているものに類似した構造を生成する必要があります:

:JSON文字列にはJavaScript resultsオブジェクトを変換

// Get a reference to our posts 
var ref = new Firebase("https://<database>.firebaseio.com/profile"); 
// some place to store stuff 
var results = { 
    'emails':[] 
}; 
// Retrieve new posts as they are added to our database 
ref.on("child_added", function(snapshot, prevChildKey) { 
    var newPost = snapshot.val(); 
    results.emails.push({ 
    'email':newPost.email 
    }); 
}); 

がするのと同じくらい簡単です

var json_string = JSON.stringify(results); 

データを配置する方法はいくつかあります。私は既知の要素のコードを提供することしかできません。

+0

ありがとうございました!構造は正しくなっていますが、どのようにしてこのオブジェクトにアクセスできますか? htmlページが読み込まれるまでにオブジェクトがまだヌルであるようです。私はHTMLでng-repeatを使用していますが、オブジェクトの人口が非常に遅くなっているようです。あなたは、オブジェクトが人口が投入されていることを確認する方法に関する提案を持っています – noobie212

+0

@ noobie212私は本当に角度に慣れていませんが、それは[この回答]ように見える(http://stackoverflow.com/questions/15891052/dynamically-add-item -to-angularjs-ng-repeat)はあなたが望むものを持っているかもしれません。 – Ouroborus

関連する問題