【发布时间】:2017-07-12 01:16:09
【问题描述】:
我正在构建一个从 Firebase 数据库获取其对象的应用。问题是当我尝试迭代我得到的东西时,它什么也没显示。但是,当我要求通过console.log 显示它时,它就在那里!
我猜这是因为 HTTP 方法及其承诺。但我还没有想出如何解决它。有人可以帮我理解吗?
HTML:
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Description</th>
<th>Price</th>
<th>Options</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="profile in profiles">
<td>{{profile.value.id}}</td>
<td>{{profile.value.name}}</td>
<td>{{profile.value.description}}</td>
<td>{{profile.value.price | currency}}</td>
<td>
<button type="button" class="btn">Edit</button>
<button type="button" class="btn btn-danger">Delete</button>
</td>
</tr>
</tbody>
</table>
Controller.js
App.controller("adminController", function($scope) {
console.log("running");
var profiles = new Array;
var query = firebase.database().ref("profiles").orderByKey();
query.once("value")
.then(function(snapshot) {
snapshot.forEach(function(childSnapshot) {
var key = childSnapshot.key;
var childData = childSnapshot.val();
profiles.push({
key: key,
value: JSON.parse(childData)
})
});
console.log(profiles);
});
}
【问题讨论】:
-
数据正在被添加到
profiles,此时 AngularJS 并不期待它。将回调包装在$timeout()中以使其知晓。在这里查看我的答案:stackoverflow.com/questions/31496266/…
标签: javascript angularjs firebase firebase-realtime-database