Seeking guidance on merging two objects retrieved using ngressource.
Every 5 seconds, I invoke my service to fetch a message and aim to append the new message with the older ones.
The JSON message I receive:
[
{"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 attempted to merge my object using 'push' and 'concat' methods but faced an issue. Corrected error (Angular says : $scope.data is undefined )
Should I perform this operation in my 'Timer' service or in my controller, and what would be the best solution?
Online demo : plnkr.co/edit/Vzdy9f7zUObd71Lm86Si
Thank you
Guillaume