0

I am loading a message list in an inbox , the most recent 5 messages in the list. I have trouble refreshing the page so that the page always shows the latest 5 messages in the list.

Message page just called the service the first time its opened. How do I recall the service ? For other services like user , I have been using $interval to update the $scope.data

Is it possible to recall service everytime it is opened?

Template

<div class="list">
  <ul ng-controller="MessagesCtrl">
    <li ng-repeat="message in messages.data" id="message{{message.id}}"  class="item">
        <a href="#" class="messageIcon">{{message.user}}</a>  
  <p>{{message.title}}</p><a ng-click="deleteItem($index)">x</a>
    </li>
</ul>

Service

angular
    .module('starter.controllers', [])
    .factory("Messages", function() {
        var Messages = {};
        return {
            getData: function($http) {
                return $http.get("http://website.com/index.php/id/user/get_message/i").
                success(function(response) {
                    /// console.log(JSON.stringify(response));
                    Messages.data = response.data;
                    return Messages;
                }).error(function(data, status, headers, config) {
                    // log error
                });
            }
        }
    });

Controller

angular
    .module('starter.controllers', [])
    .controller('MessagesCtrl', function($scope, Messages, $http) {

    Messages
        .getData($http)
        .then(function(data) {
            $scope.messages = data;
        });
});
Keshav
  • 171
  • 1
  • 11
xyonme
  • 395
  • 1
  • 7
  • 24

1 Answers1

0

You can do one of the following:

1) Poll the getdata function using a $interval in the service at regular intervals. Then write a watch on the $scope.messages in your controller and the UI will be updated everytime the value changes.

2) Use a websocket to always keep the data real-time. I am not sure this will be the easiest option out there. And might not even be scalable depending on the traffic you are expecting.

I would personally go with the first option if you are not fluent at websockets.

Aditya
  • 196
  • 14
  • I found a way to refresh the $scope.data by pull to refresh feature or button click to call the refresh function. – xyonme Jan 20 '15 at 14:14
  • Hmm. If you are alright with a user action to present updated data then this will work fine.. but if you want to display real time data always.. without a user action... then this will not work, i suppose. – Aditya Jan 20 '15 at 16:22
  • yes. It needs user action. would you please look at my new question. I cannot get access to service variable, strangely. http://stackoverflow.com/questions/28059311/angularjs-using-service-variable-for-iframe-src – xyonme Jan 21 '15 at 04:11