【问题标题】:ng-repeat doesnt loop thru my arrayng-repeat 不会循环遍历我的数组
【发布时间】:2017-08-16 14:54:34
【问题描述】:

大家好,有人可以告诉我我在这里做错了什么吗?

我试图构建一个包装器,其中我的数组中的每个元素都有一个和一个 id 但是当我遍历我的真实数组时,我只会收到文本错误:错误:[ngRepeat:dupes] ...
使用假数组,我使它完美运行。

我的 HTML:

    <div class="translaterBox">
        <span ng-repeat="person in persons" id="{{person}}">
            {{person + " "}}
        </span>

        <span ng-repeat="text in textWords" id="{{text}}">
            {{text + " "}}
        </span>
    </div>

我的脚本

var app = angular.module('splitScreenApp', []);
app.controller('splitCtrl', function ($scope, $http) {
    $scope.translatetText = "Translate Here";

    $http.get("getContent.php")
        .then(function (response) {
            var content = response.data.content;
            $scope.content = content;

            function splitContentToWords() {
                var text;
                for(var i = 0; i <= content.length;i++ ){
                    if(content[i].text){
                        var text = content[i].text;
                    }
                    return text.split(' ');
                }

            }
            $scope.persons = [
                'Jack',
                'Jill',
                'Tom',
                'Harvey'
            ];
            $scope.textWords = splitContentToWords();
            console.log($scope.textWords);
            console.log($scope.persons);
        });
});

非常感谢您的帮助

【问题讨论】:

    标签: javascript html angularjs arrays angularjs-ng-repeat


    【解决方案1】:

    当您从 Angular 收到关于欺骗的错误时,这是​​因为存在重复的键。您可以使用track by 解决此问题,如the documentation 所示。

    尝试将您的 ng-repeat 更改为:

    <span ng-repeat="person in persons track by $index" id="{{person}}">
    <span ng-repeat="text in textWords track by $index" id="{{text}}">
    

    【讨论】:

    • 非常感谢!我不得不说我仍然不明白我在哪里有重复的钥匙
    猜你喜欢
    • 2015-06-09
    • 2014-08-24
    • 1970-01-01
    • 2013-09-05
    • 2012-09-02
    • 2019-06-25
    • 1970-01-01
    • 1970-01-01
    • 2016-05-24
    相关资源
    最近更新 更多