我有这个html(要看ng-repeat
的代码段):
<ion-list show-delete="data.showDelete" id="deleteme">
<div class="list list-inset searchlist" >
<label for="searchText" class="item item-input searchlabel" >
<i class="icon ion-search placeholder-icon"></i>
<input type='text' ng-model='searchText' placeholder="Search"/>
</label>
</div>
<ion-item class="item-icon-left item-icon-right" ng-repeat="message in messageList | orderBy:'date':true | filter:searchText track by $id($index)"
ng-click="openMessage(message.id)" >
<i class="icon ion-record" style="color:#8EBF3F;font-size:15px;height:70%" ng-show='message.mesgRead == false'></i>
<i class="" ng-show='message.mesgRead == true'></i>
<h2 class="messagecontent" >{{message.mesgContent}} </h2>
<p class="messagedate">{{message.displayDate}} </p>
<i class="icon ion-chevron-right icon-accessory"></i>
<ion-option-button class="button-assertive" style="line-height:73px;" ng-click="deleteMessage(message)">
Delete
</ion-option-button>
</ion-item>
</ion-list>
这是控制器(代码片段):
.controller('MessagesCtrl', function($scope, MessagesService, $timeout, ProfileService, $window, $filter, $sce, $ionicLoading, $ionicPopup) {
$scope.messageList = "";
console.log("check for messages...");
MessagesService.loadFromMessageDB().then(function (dbMessages) {
$scope.messageList = dbMessages;
});
这样效果很好,我的ng-repeat
已填充。
我正在使用PushPlugin,因此当我收到推送通知时,会触发位于MessagesService
内的回调:
onNotificationResponse: function(sender, message, msgId, msgType, msgUrl) {
console.log("myApp.onNotificationResponse:" + message + " msgUrl:" + msgUrl);
var nowDate = new Date().getTime();
nowDate = moment(nowDate).format("ddd, Do MMM, H:mm");
var jsDate = new Date();
var tmpMessage = new myApp.Message(msgId, nowDate, msgType, sender, jsDate, message, msgUrl, false);
messages.unshift(tmpMessage);
messageDB.save(tmpMessage);
},
我的问题是我不知道如何刷新我的ng-repeat
列表。我尝试将$rootScope.$apply();
添加到onNotificationResponse
函数,但没有做任何事情。
那么如何将这条新消息添加到我的ng-repeat
列表?
更新:
将此添加到onNotificationResponse函数:
$rootScope.$broadcast('newMessage', messages);
然后再到控制器:
$scope.$on('newMessage', function(event, messages) {
console.log("WOW, a new message!!! ");
$scope.messageList = messages;
$scope.$apply();
});
有效!但是 - 这会将我的新消息添加到ng-repeat列表的底部。如何将邮件添加到顶部?
答案 0 :(得分:3)
我最终要做的就是改变这一部分:
var jsDate = new Date();
到此:
var jsDate = new Date().getTime();
根据其中一个angularJS人员的说法,getTime()返回一个在使用orderBy / sorting时效果更好的数字。
希望将来帮助别人!