【发布时间】:2016-06-21 01:18:20
【问题描述】:
我正在使用 Ionic 框架,并尝试使用以下 HTML 代码显示“卡片”元素列表:
<div ng-repeat="case in cases">
<div class="list card">
<div class="item item-avatar">
<button ng-click="rmpost(card.key)" style="background: transparent; border: none;"><i class="icon ion-close-round"></i></button><!--if (dataSnapshot.val().volunteer_email == firebase.auth().currentUser.email)-->
<h2>{{card.volunteer_name}} ({{card.volunteer_email}})</h2>
<p>{{card.title}} ({{card.creationdate}})</p>
</div>
<div class="item item-body">
<img class="full-image" src={{card.snapshot}}>
<p>{{card.message}}</p>
</div>
</div>
</div>
这是我在控制器内部用于更新案例数组的 JS 代码:
.controller('campaignslookCtrl', function($rootScope, $scope, $cordovaGeolocation, $state, $ionicPopup, Util)
{
$scope.reload = function()
{
$scope.reload();
}
$scope.rmpost = function(key)
{
alert("rmpost");
}
$scope.cases = [];
//...
firebase.database().ref('/cases')
.on('child_added', function(dataSnapshot)
{
$scope.cases.push({key: dataSnapshot.key,
volunteer_name: dataSnapshot.val().volunteer_name,
volunteer_email: dataSnapshot.val().volunteer_email,
title: dataSnapshot.val().title,
creationdate: '',//(new Date(dataSnapshot.val().creationdate)).toString(),
snapshot: dataSnapshot.val().snapshot,
message: dataSnapshot.val().message});
});
//..
})
$scope.cases 在初始化为常量值时显示良好,但在动态更新(通过 $push)时没有任何显示。
【问题讨论】:
-
您的代码似乎缺少一些关键细节。
dataSnapshot这个变量是怎么得到的? -
尝试使用
$watch并在更改时刷新。 -
@JLRishe 我更新了代码。请帮忙。
-
@VenomFangs 你能给我举个例子吗?
-
你在哪里推新的对象数组?我用超时做了一个简单的例子。 jsfiddle.net/Lvc0u55v/5652
标签: javascript angularjs ionic-framework angularjs-ng-repeat