2015-06-24 2 views
0

Так что я создал службу уведомлений в AngularJS и хорошо, что я не получаю никакого значения полных данных обратно в моей переменной $scope.Notifications.AngularJS Данные из службы

я могу увидеть обслуживание вызывается и работает в правильном интервале, и правильные данные возвращаются из 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"}]

По существу, все, что мне нужно от этого простого массива уведомлений пользователей.

Вот моя служба:

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() 
    } 
}]; 

И мой контроллер:

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

Если я утешаю войти $ scope.Notifications в контроллере я быть возврат товара это:

Object {Notifications: d}Notifications: d$$state: Objectstatus: 1value: undefined__proto__: Object$$timeoutId: 1__proto__: Object__proto__: Object 
+0

Ваш «сервис» возвращает объект с ключом '' Notifications'' –

ответ

2

Задайте свой параметр $ scope.Notifications = Notifications.Notifications;

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

В настоящее время вещь, которую вы получаете в консоли, - не что иное, как объект обещания, который является концептуально правильным. Если вам нужны данные из этого, вы используете решение, которое обещает цепочку, используя функцию .then на службе Notification.

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

Попробованного что, и теперь я получаю следующее сообщение об ошибке в моей консоли: TypeError: Notifications.then не является функция – ChrisBratherton

+0

@SeriousJelly было бы be 'Notifications.Notifications.' –

+0

Пробовал это тоже, и теперь консоль показывает $ scope.Notifications как undefined, и на моем экране нет вывода :( – ChrisBratherton

Смежные вопросы