0

How to combine two objects obtained with ngressource.

Each 5 seconds, i call my service to obtain a message and i want to add a new message with the olders.

My Json message :

[
{"age": 0,"id": "my first tweet","name": "Hello Sarah","snippet": "It's fabulous"},
{"age": 1,"id": "my second tweet","name": "Hello dude !","snippet": "It's fabulous"}
]

My Service :

'use strict';

/* Services */

var listlogServices = angular.module('listlogServices', ['ngResource']);

listlogServices.factory('Log', ['$resource',
  function($resource){
    return $resource('log/log1.json', {}, {
      query: {method:'GET', params:{}, isArray:true}
    });
  }]);

My controller and functions

'use strict';

var app = angular.module('appRecupTweetApp');


app.controller('TimerCtrl1', function TimerCtrl1($scope, Timer){

  $scope.$watch(function () { return Timer.data.myTab; },
    function (value) {
      $scope.data = value ;
      //$scope.data.push(value);
      //$scope.data = $scope.data.concat(value);
    }
  );

});

app.service('Timer', function ($timeout, Log) {

  var data = { myTab: new Array()};

  var updateTimer = function () {
    data.myTab = Log.query();
    $timeout(updateTimer, 5000);
  };

  updateTimer();

  return {
    data: data
  };
});

I try to combine my object with 'push' and 'concat' but is not right. Corrected error (Angular says : $scope.data is undefined )

Could i make this operation in my 'Timer' service or in my controller and what is the good solution.

Online demo : plnkr.co/edit/Vzdy9f7zUObd71Lm86Si

Thank's

Guillaume

Guillaume.G.
  • 1
  • 1
  • 2

1 Answers1

0

You just have to initialize $scope.data in you TimerCtrl1 controller.

 app.controller('TimerCtrl1', function TimerCtrl1($scope, Timer){
$scope.data=[];
  $scope.$watch(function () { return Timer.data.myTab; },
    function (value) {
      $scope.data = value ;
      //$scope.data.push(value);
      //$scope.data = $scope.data.concat(value);
    }
  );

});
Kavitha K Gowd
  • 527
  • 1
  • 3
  • 9