【问题标题】:ng-repeat duplicating items after page reload页面重新加载后 ng-repeat 重复项目
【发布时间】:2014-03-06 18:48:18
【问题描述】:

我有一个 angularjs 应用程序,带有 node.js 和 express 后端,所以我没有使用 angular 的路由。当我使用浏览器的刷新按钮刷新页面时,页面上的我的项目以 ng-repeat 重复加载。

我不确定为什么会发生这种情况,但我想知道当页面重新加载时控制器会发生什么?旧控制器是否已销毁,并创建了一个新控制器,或者是否添加了另一个控制器,这可能导致重复?

这是我的 html 文件的一部分,以及相关的控制器代码:

(trans.html)

<div class="pure-u-1-2" ng-repeat="trans in transactions | filter:searchText">
        <div class="transaction" ng-style="{'border-style': 'solid', 'border-color': categoryColours[trans.category.name]}">
            <h2 id="transName">{{ trans.name }}</h2>
            <h4>{{ trans.description }}</h4>
            <h4>{{ trans.date }}</h4>
            <div class="pure-u-1-1">
                <h3 id="categoryLabel" ng-style="{'background-color': categoryColours[trans.category.name], color: 'white' }">{{ trans.category.name }}</p>
                    <h2 id="transType" ng-style="{'background-color': categoryColours[trans.category.name], color: 'white' }">{{trans.type}}: £{{ trans.amount }}</h2>
            </div>
            <div class="clearFix"></div>
        </div>
    </div>
</div>

(transController)

function TransController($scope, socket) {
 var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
    $scope.transactions = [];
    $scope.currentMonth = months[getMonth()];
    $scope.categories = {};
    $scope.categoryColours = {};
    $scope.types = [{
        name: "Income"
    }, {
        name: "Spending"
    }];
    init();

    function init() {

        console.log("emitting init functs");
        monthDataRequest($scope.currentMonth, 'init');
        socket.emit('getCategories', {});
    };

    //sends a data request to the server the months data
    function monthDataRequest(month, type) {
        socket.emit('transDataRequest', {
            request: 'transactions',
            month: month,
            type: type
        });
    };

    function getMonth() {
        var d = new Date();
        return d.getMonth();
    };

    socket.on('categories', function (data) {
        var categories = [];
        for (var key in data.categories) {
            console.log("key: ", key);
            categories.push(key);
        }
        $scope.categories = categories;
        $scope.categoryColours = data.categories;
        console.log($scope.categories);
        $scope.safeApply();
    });


    socket.on('transResponse', function (data) {
        console.log("Data response received. month: ", data.month, " type: ", data.type);
        if (data.type == 'init') {
            console.log("setting up initial data");
            $scope.transactions = [];
            console.log("received data: ", data.data);
            $scope.currentMonth = data.month;
            $scope.transactions = [];
            var tran = data.data;
            for (var i = 0; i < tran.length; i++) {
                $scope.transactions.push(tran[i]);
                console.log("loop ", i, "data i: ", tran[i]);
            };

            $scope.safeApply();
            console.log($scope.transactions);
            console.log("CURRENT MONTH " + $scope.currentMonth);
        } else {
            console.log("was not initial data");
            $scope.transactions = [];
            console.log(data);
            var tran = data.data;
            for (var i = 0; i < tran.length; i++) {
                $scope.transactions.push(tran[i]);
                console.log(tran[i]);
            };
            $scope.safeApply();
            console.log($scope.transactions);
            console.log("CURRENT MONTH " + $scope.currentMonth);
        }
    });

对于刷新页面后为什么会显示重复交易的任何建议,我将不胜感激。

【问题讨论】:

  • 为什么不使用$scope.transactions = data.data 而不是for 循环?
  • 谢谢,我刚试过,更有意义,但我仍然遇到同样的问题?它在页面重新加载后重复事务 3 次
  • 使用 for 循环有很多优点,即当您有大量元素在进入/离开时动画或类似情况时,当您将整个对象分配给变量时,它会重新创建所有可能导致混乱的元素,而仅更改循环中的值不会这样做

标签: angularjs angularjs-ng-repeat angularjs-controller


【解决方案1】:

这真的很奇怪,如果你重新加载页面控制器会再次启动:

$scope.transactions = [];

将使交易无效 - BTW transResponse

$scope.transactions = [];
console.log("received data: ", data.data);
$scope.currentMonth = data.month;
$scope.transactions = [];

你在没有任何其他操作的情况下将其设为 null 两次,我认为你至少可以省略其中一个

此时我唯一的猜测是套接字为您提供重复的事务,或者您以某种方式将其存储在服务中(由于您没有提供套接字服务代码,因此无法判断)

你可以直接在 angularjs IRC 频道 irc.freenode.net #angularjs -> maurycyg 上找到我

【讨论】:

  • 谢谢,试图修复这个错误有点绝望。还有其他方式可以联系您/展示我的服务器端实现吗?
  • Google Talk 也许,在我的昵称末尾添加 [g] 以获取我的电子邮件
  • 嘿,我面对的是same issue。你能在哪里解决这个问题?
  • 如果你可以在 plunker、codepen、jsfiddle 上重现它,我可以很容易地为你调试它,我很想找到背后的问题,以前的 OP 对此没有帮助;)
猜你喜欢
  • 1970-01-01
  • 2014-11-27
  • 1970-01-01
  • 2020-06-25
  • 2018-07-22
  • 1970-01-01
  • 2017-01-13
  • 1970-01-01
  • 2016-12-21
相关资源
最近更新 更多