角度分页控制器

时间:2018-10-18 20:50:03

标签: javascript angularjs pagination controller factory

我正在申请工作。我是一名开发人员,但是他们看到了我的简历并向我发送了AngularJS问题。 这个问题是否可以解决?或者他们只是在拉扯我的连锁店?有人可以将我定向到可以学习回答此问题的资源吗?

要求:

在AngularJS中编写一个使用通知服务来获取用户通知的控制器。您应该能够返回10条通知,使用分页获取以下10条通知,然后继续进行操作,直到没有更多结果为止。

通知参数: -偏移量:要跳过的通知数量 -限制:要返回的通知数量 -from:时间戳记,从开始算起的毫秒数,用于分页,因此新通知不会影响结果。

-------->工厂服务

'use strict';

angular.module('app')

.factory('NotificationService', function ($resource) {
  return $resource('/api/me/notifications/:controller', {
    id: '@_id'
  },
  {
    notifications: {
      method: 'GET'
    },
    getSettings: {
      method: 'GET',
      params: {
        controller: 'settings'
      }
    },
    setSettings: {
      method: 'POST',
      params: {
        controller: 'settings'
      }
    }
  });
});

-------->控制器

'use strict';

(function () {

  function NotificationController() {

  }

  angular.module('app').controller('NotificationController', NotificationController);

})();

1 个答案:

答案 0 :(得分:0)

我希望其他人可以发表评论并引导我朝正确的方向前进。 到目前为止,我不确定是否应该更改工厂,并假定分页将在查询中完成。任何帮助谢谢!

(function () {
// assuming the current page, offset and limit are defined by the user settings
  function NotificationController($scope, $resource, NotificationService) {
    // declaring things i might need
    let notifications = NotificationService.notifications();
    let totalNotes = notifications.length;
    let id = $scope.id;
    let offset = $scope.offset;
    let startTime = $scope.starTime;
    let pageIterator = $scope.pageIterator;
    let pages = Math.ceil(totalNotes / pageIterator);

    // check if grabbed some undefined items from database
    function checkUndef(notes){
        let newNotes = notes;
        if (notes[notes.length - 1]=== undefined ){
            newNotes.pop();
            return checkUndef(newNotes);
        } else return newNotes;
    }
    // filterfor time
    function checkTime(note) {
       if( note.createAt > startTime) {
           return note;
       }
        )
    }
    // finally running the functions
    notifications = checkUndef(notes);
    notifications = notifications.filter(checkTime); 
    return notifications.slice($scope.offset, $scope.offset+$scope.pageIterator);
        
  }

  angular.module('app').controller('NotificationController', [$scope,$resource, NotificationController]);

})();