Angular ng-repeat:构造新对象

时间:2016-01-09 14:46:23

标签: javascript angularjs

var $scope.notif包含此数据:

enter image description here

在弹出窗口中,我想查看不同的消息,因此我使用了:

<ion-list><ion-item ng-repeat="n in notif">{{n.msg}}</ion-item></ion-list>

当消息1和消息2存在时,还有3个空列表项,可能是因为msgmsg_titlenotif_type数据的长度为5。

示例显示空列表项here

我可以告诉Angular重复长度减3吗?

我尝试使用以下方法删除对象中的3个额外项:

    for(var i=0; i<($scope.notif.length); i++) {
            $scope.notifnew.push($scope.notif[i]);
    }

$scope.notifnew现在是undefined ...

2 个答案:

答案 0 :(得分:1)

您可以过滤掉您不喜欢的属性:

 $.each(results.data, function(index) {
        var item = results.data[index];
        geocoder.geocode( { 'address': address}, function(results, status) {
            item.lat = results[0].geometry.location.lat();
            item.lng = results[0].geometry.location.lng();
        });
    });

updated your plunker

答案 1 :(得分:1)

$scope.notif.length无效,因为$scope.notif是一个对象而不是一个数组。您可以使用以下内容进行迭代:

var messages = []; 
angular.forEach($scope.notif, function(val, key){

    if (!isNaN(parseInt(key))) {
        messages.push(val);
    } 
})

但这种结构对我来说似乎“破碎了”。此外,有效载荷应该是一个数组imo。

var test = {
    "user_ids":["...","..."],
    "notification":
    {
        "alert":"dummy",
        "android":{
            "title":"New alert 2",
            "payload":{
                "msg":"lala",
                "msg_title":"Thanks!",
                "notif_type":"alert",
                "0":{
                    "msg":"Message 1"
                },
                "1":{
                    "msg":"Message 2","msg_title":"Thanks!"
                }
            }
        }
    }
}