Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

limitTo not working in ng-repeat in AngularJs

I am working out some code for some application. I want to limit the messages in the chat

this.limitM = -10;
$scope.msgsCount = //contains the number of messages


<button ng-if="msgsCount > -main.limitM" 
ng-click="main.limitM=-10+main.limitM">Load More</button>

<li class="singleMessage clearfix" 
    ng-repeat="msg in chat.msgs | limitTo:main.limitM" 
    ng-class="::{myMessage: msg.senderName != chat.name}">

<img class="profileImage" alt="::{{chat.name}}" width="40px">
    <p ng-bind-html="parseMsg(msg)"></p>
</li>

This is not limiting the messages, all the messages appear. Can anyone help ?

like image 351
firebolt_ash Avatar asked Dec 25 '22 22:12

firebolt_ash


1 Answers

First you have to define all necessary variables to make it work.
Example of your controller:

$scope.limit = 3;
$scope.expand = function(limit) { 
  $scope.limit += limit;
}
$scope.chat = ['one', 'two', 'three', 'four', 'five', 'six', 'seven'];

And view:

 <button ng-if="chat.length > limit" ng-click="expand(limit)">Load More</button>
 <li ng-repeat="msg in chat | limitTo : limit" >{{ msg }}</li>

Here is a simple plunk, that does what you need.

like image 194
Andrii Avatar answered Dec 28 '22 11:12

Andrii