代码之家  ›  专栏  ›  技术社区  ›  user7620991

Angularjs在使用routeProvider时自动刷新数据

  •  0
  • user7620991  · 技术社区  · 7 年前

    init() 获取控制器中的数据。如果我使用 初始化()

        App.controller('PlantCtrl', [ '$scope', 'PlantDetails', '$interval'
    , function($scope, PlantDetails,  $interval,) {
    
    $scope.availablePlantDetailsTemp = [];
        $scope.availablePlantDetails = [];
    
    init();  
    
        function init() {
            $scope.availablePlantDetailsTemp = PlantDetails.resource2.query(function(response) {
            angular.forEach(response, function(item) {
                if (item) {
                    $scope.availablePlantDetails.push(item);
                }
            });
            });
                }
    $interval(function() {
                    init();
                    }, 5000);
    }  ]);
    

    1 回复  |  直到 7 年前
        1
  •  2
  •   Ved    7 年前

    在向数组添加新值之前,您需要清除数组。大堆 push 方法向数组中添加新值,因此您面临这个问题。

    function init() {
            $scope.availablePlantDetails = [];
            $scope.availablePlantDetailsTemp = PlantDetails.resource2.query(function(response) {
            angular.forEach(response, function(item) {
                if (item) {
                    $scope.availablePlantDetails.push(item);
                }
            });
            });
    
    $interval(function(){
     Init()
    },1*2000)