Icultlive  Icultlive
Icultlive Icultlive

Reputation: 143

transfer logic into controller

please help to fix the script. http://jsfiddle.net/rrnJc/

this is a dynamic list of news. you can scroll through.

the problem that the logic is in the template:

<li ng-repeat="item in news" ng-show="$index >= currentPosition && $index <= (currentPosition + qntVisibleRecords)">

this is not right.

I would like to chtby visible part of the list of news was in the controller (in the function $scope.newsVisible). at the same conclusion in the template design and deliver this:

<li ng-repeat="item in newsVisible">
    <span class="date">{{item.date}}</span>

    <span class="title">{{item.title}} - {{$index}}</span>
</li>

Upvotes: 0

Views: 39

Answers (1)

Nicolas ABRIC
Nicolas ABRIC

Reputation: 4935

Just slice your array of news when controller is initialized and each time you call the changeCurrent function and iterate through newsVisible. Possible code could be :

$scope.changeCurrent = function(value){
  $scope.currentPosition = $scope.currentPosition + value;
  if($scope.currentPosition < 0 ){
     $scope.currentPosition = 0;
  }
  var end = Math.min($scope.currentPosition + $scope.qntVisibleRecords, $scope.news.length)
  //ensure that you won t slice at an index greater than news.length

  $scope.newsVisible = $scope.news.slice($scope.currentPosition, end);
}

BTW you can call this function when the controller initialized in order to init your array of newsVisible.

 $scope.changeCurrent($scope.currentPosition);

You can check it here http://jsfiddle.net/rrnJc/1/

Upvotes: 1

Related Questions