我正在尝试为我的Ionic + Firebase应用做一个自定义角度过滤器。
我的观点是在"聊天"中重复使用firebase的用户标签。我想过滤它,所以它只显示已经与之交谈过的用户。
我试图在视图中调用filter,它返回错误:TypeError: Cannot read property 'length' of undefined
。当我在控制器中调用它时,它显示空白页面,没有错误。我已经被困在这里很长一段时间了。
查看:
<ion-view view-title="Chats">
<ion-content>
<ion-list >
<ion-item class="item-remove-animate item item-thumbnail-left" ng-repeat="user in users | chatfilter" type="item-text-wrap" href="#/tab/chats/{{ownId}}&ID={{user.id}}" id = "chatlist" >
<img src="{{user.profilepic}}">
<h2 id="nameId" ng-model = "nameid"> {{user.name}} <i class="icon ion-chevron-right icon-accessory"></i></h2>
</ion-item>
</ion-list>
</ion-content>
</ion-view>
过滤器:
.filter("chatfilter", function (Auth, $firebaseArray){
return function(items){
var filteredUsers = []; //I want to push users I've talked to here
Auth.$onAuth(function(authData) {
var ownid = authData.uid;
var messages = new Firebase("https://newbuddy.firebaseio.com/messages");
var i = items.length;
var latestSnapshot = null;
var filtered = [];
// RegExp will check all messeges references, math those with logged in user's id, and push them to filtered.
messages.on('child_added', function(snap) {
latestSnapshot = snap.key();
var patt = new RegExp(ownid.toString());
var res = patt.test(latestSnapshot);
if (res == true) {
var receiver = latestSnapshot.replace(patt, "");
filtered.push(receiver);
}
// loop will test ids from filtered against all users and push mathes into filtratedUsers.
for (var j = 0; j < i; j++){
if (filtered.indexOf(items[j].id) != -1) {
filteredUsers.push(items[j].id);
}
}
})
return filteredUsers;
});
};
})