【问题标题】:Angular pagination controller角度分页控制器
【发布时间】:2018-10-18 20:50:03
【问题描述】:

我正在申请一份工作。我是一名 React 开发人员,但他们看到了我的简历并向我发送了 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);

})();

【问题讨论】:

  • 问问谷歌就行了。告诉我们,到目前为止你尝试了什么?人们不是来为你解决任务的,如果你在某个时候卡住了,他们可以提供帮助,或者他们可以提供更好的路径。
  • 我不是在找人来解决它。我在问是否有可能用给定的信息来解决。我对角度一无所知(我是一个反应开发者),我想知道他们是否只是在逗我。
  • @ChristopherLin 我认为这个问题很简单。他们被问到一个问题来列出所有通知(通知可能在数据库中)并显示在网格或其他一些控件中。但是不要硬编码任何值,例如每个分页上的记录数、偏移量等。将来它可能会改变。所以你的代码应该是非常可扩展的。我认为他们可能会寻找那些东西。您的代码质量
  • @ChristopherLin 另外,角度服务只不过是编写可重用的逻辑(可以是数据库服务或客户端逻辑),并且可以作为依赖项传递给组件或控制器。
  • 谢谢 Jameel,我会在周末学习 Angular 并回答问题。

标签: javascript angularjs pagination controller factory


【解决方案1】:

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

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

})();

【讨论】:

    猜你喜欢
    • 2015-09-10
    • 1970-01-01
    • 2013-05-12
    • 2013-08-15
    • 2013-08-16
    • 2014-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多