migrate the logic to the controller

can someone assist in rectifying the code issue? http://jsfiddle.net/rrnJc/

this module features a dynamic news list which can be scrolled through.

the current setback lies within the template logic:

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

this needs improvement.

my aim is to have the visible section of the news list handled in the controller (via the function $scope.newsVisible). subsequently, this should be reflected and executed in the template as follows:

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

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

Answer №1

To ensure your array of news is properly sliced, initialize the controller and call the changeCurrent function each time you iterate through newsVisible. Consider using the following code:

$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)
  //make sure not to slice beyond news.length

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

You can also initialize this function when the controller is initialized to set up your array of newsVisible.

 $scope.changeCurrent($scope.currentPosition);

See it in action here: http://jsfiddle.net/rrnJc/1/

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The $multiply function is only compatible with numerical data types, not arrays

Attempting to utilize the $multiply operator within MongoDB. STEP 1 db.message1064sd_00011_3744.aggregate([ {$project : { "prices.00026" : 1, priceReal : { price : "$prices.00026", } }}, { $match : { ...

What to do when the 'image' property in Next.js next/image has an implicit 'any' type and needs fixing?

I'm a newcomer to Next.js and TypeScript and I can't seem to find any helpful resources on resolving this particular issue: import Image from 'next/image'; export default function Item({ image }) { // <-- parameter image needs a &ap ...

Obtain the language of a Wordpress page using javascript

Is there a way to determine the language of a Wordpress page using Javascript? I have discovered a method to detect Spanish: if(window.location.href.indexOf("/es/") > -1) { However, if the website does not use Permalink Settings with "Post name", th ...

Removing Component Items in a Vue.js v-for Loop

I recently created a Vue radio button component and I'm having trouble figuring out how to delete the selected values. While my current solution deletes the actual values, the selection still remains visible. Below is the code for the component: < ...

Generating pages dynamically based on the number of divs

So, I've been using template literals to create and append new divs, but now I want to take it a step further. Is there a way to automatically generate a 'next page' when a certain number of divs have been appended in HTML & JS? Appreciate ...

Is it possible to swap out images using srcset attribute?

Currently, I am facing an issue regarding changing the img on mobile and tablet devices to display different images. As a beginner with jQuery, I couldn't resolve it using that framework, so I am exploring options with html5. I am wondering if there ...

How can I create clickable table entries using php and html?

I want to create a table on this page that is clickable. When the user clicks on a row, the data from that row will be sent to a PHP file with a form prepopulated with the selected user's information for updating purposes. <!DOCTYPE html> &l ...

Detecting hidden child divs due to overflow: hidden in Angular6

Below is the particular issue I am aiming to address. <div class="row" style="overflow:hidden;"> <app-car *ngFor="let car of cars; trackBy: trackByFunction" [car]="car" > </app-car> </div> <button> ...

.NET Core ViewModel with IFormFile attribute causing AJAX Request to freeze

Users now have the option to upload images for a retail product within an Add/Edit Product modal. Product Modal ViewModel: public class ProductModalViewModel { public ProductModalViewModel() { Product = new ProductDTO(); Images = ...

What is the best way to delete markers from a leaflet map?

I need to remove markers from my map. I am looking to create a function that will specifically clear a marker based on its ID. I am utilizing Leaflet for the map implementation. Here is my function: public clearMarkers(): void { for (var id in this. ...

Utilizing NGINX as a reverse proxy for secure communication with Node.js

I'm currently running an NGINX server setup as a reverse-proxy server for a node application. I am now trying to enable HTTPS, but every time I attempt to access the site via HTTPS, I encounter a "502: Bad Gateway" error. server { listen 80; ...

Obtain the xlsx file transferred from a Node.js backend to an AngularJS frontend

I have successfully generated an Excel file from a JSON array of objects using json2xls in Node.js. The file is correctly formatted and stored on the server, but not in a public folder. Now, I am trying to send a response to the frontend so that users can ...

Creating Global Variables in Node/Express Following a Post/Get Request

How can I dynamically pass the payment ID received after a POST request to a subsequent GET request in Node/Express for a payment system? Below is an example code snippet from my project: let paymentId; app.post("/api/payments", (req, res) => ...

Oops! There seems to be an error in my code - it's expecting a semicolon. I'm currently working on developing an authentication system using credentials with next auth

#pages/api/auth I'm currently in the process of developing an authentication system using Nextauth in Next Js. I encountered an issue with authenticating using credentials while following a tutorial on YouTube.I've double-checked the syntax multi ...

Encountering an unusual hash code when implementing Google Tag Manager in a Next.js project was

I am currently using Next.js and have added Google Tag Manager through a script <script dangerouslySetInnerHTML={{ __html: `(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start': new Date().getTime(),event:'gtm.js'});var ...

Make an HTTP request to a different domain using AngularJS

I have been working on creating an API using django-rest-framework. However, I encountered an error when running a virtual web server on localhost and attempting to send a request to the API. XMLHttpRequest cannot load http://127.0.0.1:8000/users?format= ...

Ensure jQuery waits until the function has completed execution

Here is the script in question: var d; $(".circle").click(function() { var id = $(this).attr('id'); var MyDiv1 = document.getElementById(id); var name = MyDiv1.innerHTML; $.get("url", function(data){ d=data; }); d=d ...

Having issues with jQuery when trying to select only a single checkbox?

I have created a table with four rows and eight columns, each containing a checkbox. My goal is to allow only one checkbox to be checked per row. I am attempting to achieve this using jQuery. While it works in jsfiddle, I am experiencing difficulties in ge ...

The checkbox appears to be in an updated state, however, in reality, it has not been updated

Encountering a peculiar issue while working on something for Magento. I have implemented a feature that allows me to select all categories with a link and jQuery event. The functionality works perfectly, but for some reason, the html does not update proper ...

What is the best way to include attributes in an HTML element?

I've been researching how to dynamically add attributes to an HTML tag using jQuery. Consider the following initial HTML code: <input type="text" name="j_username" id="j_username" autocorrect="off" autocapitalize="off" style="background-image: lin ...