Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
92
rated 0 times [  97] [ 5]  / answers: 1 / hits: 34134  / 11 Years ago, wed, april 24, 2013, 12:00:00

i'm currently working on a web based twitter client and therefore i used angular.js, node.js and socket.io. i push new tweets via socket.io to my client, where a service waits for new tweets. when a new tweet arrives, the service send an event via $broadcast.



in my controller is a event listener, where incoming tweets are being processed in an seperate function. this function simply pushes the new tweet in my tweet scope.



now, my problem ist, that my view is not updating but i can see my scope growing. maybe one of you have an idea, how i can solve this?



additionally my code:



service:



(function () {
app.factory('Push', ['$rootScope', function ($rootScope) {
socket.on('new-tweet', function (msg) {
$rootScope.$broadcast('new-tweet', msg);
});
}]);
}());


controller:



(function () {
app.controller(StreamCtrl, function StreamCtrl ($scope, $rootScope, $http, Push) {
$scope.tweets = [];

$http
.get('/stream')
.then(function (res) {
$scope.tweets = res.data;
});

$scope.addTweet = function (data) {
$scope.tweets.push(data);
console.log($scope.tweets);
};

$rootScope.$on('new-tweet', function (event, data) {
if (!data.friends) {
$scope.addTweet(data);
}
});
});
}());


the whole project is here: https://github.com/hochitom/node-twitter-client


More From » angularjs

 Answers
11

Adding below line of code in addTweet and the problem would be solved



$scope.addTweet = function (data) {
$scope.tweets.push(data);
$scope.$apply();
console.log($scope.tweets);
};

[#78666] Tuesday, April 23, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
joanneamiyaa

Total Points: 532
Total Questions: 127
Total Answers: 98

Location: Guam
Member since Tue, Nov 3, 2020
4 Years ago
;