0

So I have created a Notifications service in AngularJS and well I'm not getting any meaning full data back in my $scope.Notifications variable.

I can see the service is being called and running at the correct interval, and the correct data is being returned from the API:

[{"id":1,"user_id":1,"content":"You have new mail: Test","read":null,"type":"mail","deleted_at":null,"created_at":"2015-06-23 20:16:38","updated_at":"2015-06-23 20:16:38"},{"id":2,"user_id":1,"content":"You have new mail: Test","read":null,"type":"mail","deleted_at":null,"created_at":"2015-06-23 20:16:38","updated_at":"2015-06-23 20:16:38"},{"id":3,"user_id":1,"content":"You have new mail: Test","read":null,"type":"mail","deleted_at":null,"created_at":"2015-06-23 20:16:38","updated_at":"2015-06-23 20:16:38"}]

Essentially, all I need from this is a simple array of the users notifications.

Here is my service:

app.services.Notifications = ['$http', '$timeout', function($http, $timeout){

    var timeoutId;
    var notificationService = this;

    function checkForNotifications(){
      console.log('checking')
      return $http.get('/api/notifications')
        .then(function(res){
              return res.data.filter(function(notification){
                  return notification.unread === true;
              })
        })
        .then(function(unreadNotifications){
          //fake for effect
          notificationService.count = Math.floor(Math.random() * (100 - 1)) + 1;
          //notificationService.count = unreadNotifications.length;
        })
        .then(waitAndCheck)
    }

    function waitAndCheck(){
      return $timeout(function(){
        checkForNotifications()
      },5000);
    }

    return {
        Notifications: waitAndCheck()
    }
}];

And my controller:

app.controllers.notificationsController = ['$scope', 'Notifications', function($scope, Notifications) {
    $scope.Notifications = Notifications;
}];

If I console log $scope.Notifications in the controller I being return this:

Object {Notifications: d}Notifications: d$$state: Objectstatus: 1value: undefined__proto__: Object$$timeoutId: 1__proto__: Object__proto__: Object
1
  • Your "service" returns an object with the key Notifications Commented Jun 24, 2015 at 20:12

2 Answers 2

2

Set your $scope.Notifications = Notifications.Notifications;

app.controllers.notificationsController = ['$scope', 'Notifications', function($scope, Notifications) {
    $scope.Notifications = Notifications.Notifications;
}];
Sign up to request clarification or add additional context in comments.

Comments

0

Currently the thing which you are getting in console is nothing but a promise object which is conceptually correct. If you want the data from it then you use resolve that promise chain using .then function on Notification service.

Notifications.Notifications.then(function(data){
   $scope.Notifications = data;
}) 

8 Comments

Tried that, and now I get the following error in my console: TypeError: Notifications.then is not a function
@SeriousJelly it would be Notifications.Notifications.
Tried that too, and now the console is showing $scope.Notifications as undefined and there is no output to my screen :(
@SeriousJelly seems like you json doesn't have unread value which is filtering noting notification.unread === true here
Ah, yea that makes sense, ermmmm, still if I show {{ Notifications }} in my view, I am getting nothing, Scope also shows as undefined at the first instance (when the app is loaded)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.