如何实现AngularJS&amp ;;的无限滚动MVC

时间:2015-01-01 10:51:15

标签: angularjs asp.net-mvc-4 angularjs-ng-repeat

我使用MVC 4创建了一个角度JS应用程序

我在其中创建了一个渲染模板的视图,因为我们有一个包含大量数据的模板,因为我想要实现无限滚动的缺少记录

1.index.cshtml

<div id="sidebar-left" class="span2">
    <div class="nav-collapse sidebar-nav">
        <ul class="nav nav-tabs nav-stacked main-menu">
            <li class="navbar-brand"><a href="/Events/Talks">Talks</a></li>
            <li class="navbar-brand"><a href="/Events/SRDNames">SRDNames</a></li>
            <li class="navbar-brand"><a href="/Events/Speakers">Speakers</a></li>
            <li class="navbar-brand"><a href="/Events/AddTalk">Add Talk</a></li>
        </ul>
    </div>
</div>
  1. SRDNames.cshtml

      <div class="box-content">
        <table class="table table-striped table-bordered bootstrap-datatable datatable">
        <tr>
            <th>
                SRD_NAME
            </th>
            <th>
                CREATED_BY_USER_ID
            </th>
        </tr>
        <tr ng-repeat="srdname in srdnames">
            <td>
                {{srdname.sRD_NAME}}
            </td>
            <td>
                {{srdname.cREATED_BY_USER_ID}}
            </td>
        </tr>
    </table>
    

  2. 3.eventModule.js

       var eventModule = angular.module("eventModule", []).config(function ($routeProvider, $locationProvider) {
            //Path - it should be same as href link
            $routeProvider.when('/Events/Talks', { templateUrl: '/Templates/Talk.html', controller: 'eventController' });
            $routeProvider.when('/Events/Speakers', { templateUrl: '/Templates/Speaker.html', controller: 'speakerController' });
            $routeProvider.when('/Events/AddTalk', { templateUrl: '/Templates/AddTalk.html', controller: 'talkController' });
            $routeProvider.when('/Events/SRDNames', { templateUrl: '/Templates/SRDNames.html', controller: 'srdnamescontroller' });
            $locationProvider.html5Mode(true);
        });
    
    1. srdnamescontroller.js

      eventModule.controller("srdnamescontroller", function ($scope, EventsService) {
      EventsService.getSRDName().then(function (srdnames) { $scope.srdnames = srdnames }, function ()
      { alert('error while fetching talks from server') })
      

      });

    2. 5.EventsService.js

      eventModule.factory("EventsService", function ($http, $q) {
          return {
      
              getSRDName: function () {
                  // Get the deferred object
                  var deferred = $q.defer();
                  // Initiates the AJAX call
                  $http({ method: 'GET', url: '/events/GetSRDName' }).success(deferred.resolve).error(deferred.reject);
                  // Returns the promise - Contains result once request completes
                  return deferred.promise;
              },
      
      });
      

      希望在上述应用程序中实现http://jsfiddle.net/vojtajina/U7Bz9/ ..请帮助

1 个答案:

答案 0 :(得分:0)

Demo

有许多可能的解决方案。这是一个可能适合你的。 实现定义以下内容的滚动模块:

  1. infiniteScroll指令
  2. 获取可滚动数据的数据服务
  3. 您可以在应用中使用滚动模块:

    <强> HTML:

    <div ng-app="app" ng-controller="ctrl">
        <div infinite-scroll="items">
        </div>
    </div>
    

    <强> JS:

    var app = angular.module('app', ['scroll']);
    app.controller('ctrl', function($scope, dataService) {
        $scope.items = [];
        dataService.loadMore($scope.items, function(lastItem) {
            var items = [];
            var id = lastItem ? lastItem.id : 0;
            for (var i = 0; i < 5; i++) {
                items.push({id: id + i});
            }
            return items;
        });
    });
    

    dataService公开了一个接受数组的loadMore方法,以及一个用于加载更多数据的回调函数。上面的示例通过循环5个项目并添加到数组来加载更多数据。但您可以自定义此函数回调以从其他服务检索数据:

    var app = angular.module('app', ['scroll']);
    app.controller('ctrl', function($scope, $http, dataService) {
        $scope.items = [];
        dataService.loadMore($scope.items, function(lastItem, done) {
              var lastItemId = lastItem ? lastItem.id : '';
              $http({ method: 'GET',url:'api/items/' + lastItemId})
                   .success(function(items) {
                       done(items);
                    });            
        });
    });