首页 文章

返回重复数组之前的Angular 'duplicates in repeater'错误

提问于
浏览
0

我刚刚开始在Angular.js中看到这个错误(1.5)

Error: Duplicates in a repeater are not allowed. Repeater: day in forecast key: string:o
at ngRepeatAction (url...)

奇怪的是,在返回预测数组之前,此错误会显示在控制台中 .

返回数组时,它是一个唯一的数组

[{"print_date":"Tue","long_day":"Tuesday","weather":{"conditions":"Clouds","temps":{"current":19,"max":20,"min":16}}},
{"print_date":"Wed","long_day":"Wednesday","weather":{"conditions":"Clouds","temps":{"current":22,"max":24,"min":15}}},
{"print_date":"Thu","long_day":"Thursday","weather":{"conditions":"Clouds","temps":{"current":22,"max":22,"min":18}}},
{"print_date":"Fri","long_day":"Friday","weather":{"conditions":"Rain","temps":{"current":22,"max":23,"min":18}}},
{"print_date":"Sat","long_day":"Saturday","weather":{"conditions":"Rain","temps":{"current":22,"max":23,"min":15}}}]

预测数组是通过构建的

$scope.forecast = (function(){
        var day_list=[];
        for(var d in forecast.data.list){
        var day_weather = formatForecast(forecast.data.list[d],date);
        day_list.push(day_weather);
        }
        return day_list;
    })();

2 回答

  • 0

    当你只想访问数组中的成员时,永远不要在for循环中执行 . 当您浏览对象的属性时,应该使用for循环,而不是数组的成员 . 看看当你做一个正常的循环时会发生什么 . 您的格式预测可能会创建两个相同的对象

  • 0

    事实证明,我已将$ scope.forecast定义为'加载天气',作为字符串,然后用数组替换它,因此angular将字符串转换为数组,然后注意到有重复的字母 .

    我这个愚蠢的错误,希望这有助于其他人 .

相关问题