简体   繁体   English

想要从$ scope数组中添加和减去项目

[英]Want to add and subtract items from $scope array

I am using $scope.exchange to send data to an ng3 chart. 我正在使用$ scope.exchange将数据发送到ng3图表。 I want the chart to add a new value and delete the oldest value. 我希望图表添加一个新值并删除最旧的值。 So I never have more than 3 points. 所以我从来没有超过3分。 It currently adds 3 points every second but never removes any. 目前,它每秒增加3点,但从不删除任何点。 If I change the line with slice to $scope.exchange = [], the chart will draw the first 3 and never update again. 如果我将带有slice的线更改为$ scope.exchange = [],则图表将绘制前3个,并且不再更新。 If I use the while loop that is commented out, then the chart re-draws everything every second. 如果我使用了已注释掉的while循环,则图表每秒都会重绘所有内容。 This flashes the screen and looks ugly. 这会闪烁屏幕,看起来很难看。 What can I do? 我能做什么?

(function(){
var app = angular.module('exchange-directives', []);

app.directive('tableRows', function() {
    return {
        restrict: 'E',
        templateUrl: "js/directives/tableRow.html",
        scope:'=',
        link: function(scope, elements, attrs){
        },
        controller: ['$http', '$timeout', '$scope', function($http, $timeout, $scope) {
          var table = this;
            table.rows = [];
            var count = 0;
            var retrieveItems = function() {
                if($scope.exchange.length != 0) $scope.exchange.slice(3);
                //while($scope.exchange.length){
                //  $scope.exchange.pop();
                //}
                $http.get('/dashboard/history').success(function(data){//change back to '/dashboard/rates' when aws goes back online
                    table.rows = data;
                    for (i = 0; i < 3; i++){
                        var vals  = {
                            x: count++, 
                            dates: data.dates[i],
                            rates: data.rates[i]
                  }
                        $scope.exchange.push(vals);
                    }
                    $timeout(retrieveItems, 1000);
            });
            }
            retrieveItems();

        }],
        controllerAs: "table", 
    };
});
})();

It'd be easier with a fiddle, but here's what I think you want. 摆弄小提琴会容易一些,但是我想这就是您想要的。 First off, I'd "unshift" this new info instead of "pushing" it. 首先,我将“取消转移”这个新信息,而不是“推送”它。 It looks to me like you're currently adding the data to the end, then deleting that same data. 在我看来,您当前正在将数据添加到末尾,然后删除相同的数据。 Then I'd pop() the end item, instead of assuming that the object has 3 items. 然后,我将pop()作为结束项,而不是假设该对象具有3个项。 Next, I'd pass in the number of items you want to load. 接下来,我输入您要加载的项目数。 I'm not sure how the table.rows array is being used, but I'd guess you want to unshift that as well. 我不确定table.rows数组的使用方式,但是我想您也想取消这一操作。

var retrieveItems = function(num) {
    num = num || 1;
    if($scope.exchange.length != 0) $scope.exchange.pop();

    $http.get('/dashboard/history').success(function(data){
        table.rows.unshift(data);
        for (i = 0; i < num; i++){
            var vals  = {
                x: count++, 
                dates: data.dates[i],
                rates: data.rates[i]
            }
            $scope.exchange.unshift(vals);
        }
        $timeout(retrieveItems, 1000);
    });
}
retrieveItems(3);

Hope this helps. 希望这可以帮助。

@Shashank was on the right track everything was creating a new object that the rest of my code could not accept. @Shashank步入正轨,一切都在创建一个我的其余代码无法接受的新对象。 pop() just mutates the current array and therefore is accepted by all parts of the code. pop()只是改变当前数组,因此被代码的所有部分接受。 So I had to do 所以我要做

this.setHistory = function(input) {
    while($scope.exchange.length){
    $scope.exchange.pop();
    }
    $scope.history = input;
    startup(input, $scope.historyData);
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM