Angularjs ng-repeat根据控制器中的变量重复

时间:2015-08-23 05:58:20

标签: javascript angularjs angularjs-directive

这是局部视图。 sntncs = thetext.Split(New String() {sperator}, StringSplitOptions.RemoveEmptyEntries) 重复包含50个电子邮件记录的json文件。

Ng-repeat

<table class="table table-hover" ng-controller="emailViewController"> <tbody data-ng-controller="settingsController"> <tr ng-repeat="email in emails" > <td><input type="checkbox" ng-checked="checkAllEmail" ng-model="selectedEmail"/> <a href="#"> <span class="glyphicon glyphicon-star-empty"></span> </a></td> <td><label ng-bind="email.from"></label></td> <td><label ng-bind="email.subject"></label></td> <td><label ng-bind="email.time"></label></td> </tr> </tbody> </table>

settingsController.js

我无法确定如何根据(function() { 'use strict'; var settingController = function (fetchDataService, $scope, savePreferenceService, $localStorage) { $scope.url = 'app/mock/settings.json'; $scope.save = {}; fetchDataService.getContent($scope.url) .then(function(response){ $scope.contacts = response.data.contacts; $scope.languages = response.data.languages; $scope.conversations = response.data.conversations; $scope.undoSend = response.data.undoSend; $scope.save = response.data.userPreferences; }); $scope.setPreference = function () { savePreferenceService.setPreferences($scope.save.selectedLang, $scope.save.converse, $scope.save.selectedNumber, $scope.save.selectedNumberContact, $scope.save.reply, $scope.save.signature); } $scope.conversation = $localStorage.selectedNumber; }; angular.module('iisEmail') .controller ('settingsController', ['fetchDataService', '$scope', 'savePreferenceService', '$localStorage', settingController]); }()); 的值来ng-repeat迭代JSON文件。因此,例如,如果$scope.conversation为10,我希望$scope.conversation只迭代10次。我不想显示剩余的40封电子邮件。有没有人对如何实现这个功能有任何想法?

更新

在@Prashank的评论的帮助下,我明白了。以下是使用ng-repeat过滤器的代码。

limitTo

2 个答案:

答案 0 :(得分:0)

您可以使用limitTo或slice并执行以下操作,

这是一个样本,

使用limitTo:

   <div ng-repeat="item in items | limitTo:needed">
           {{item.name}}
     </div>

limitTo

答案 1 :(得分:0)

有两种方法可以做到这一点。

更容易方式是在ng-repeat中使用 $ index 隐藏/显示项目,如下所示:

<tr ng-repeat="email in emails" ng-hide="conversation.length() < $index">
    //same as in the question        
</tr>

我这样做的方式(虽然有点冗长)使用过滤器,如下所示:

<强> HTML:

<tr ng-repeat="email in emails | conversationFilter: conversation">
    //same as in the question        
</tr>

过滤

app.filter('conversationFilter', function() {
    return function(collection, conversation) {
        return collection.slice(0, conversation);
    }
})