Clicking to Load Images - Angular

Implementing a feature to load sets of images on button click instead of loading all at once. Although lazy load plugins are available, I decided to experiment with this approach.

Here's the logic: Start with a data array called 'Images' containing 30 images. Create a temporary array 'temp' bound to the list item and populate it with a set of 4 images from 'Images' upon button click.

However, when I click the button, the images briefly appear and then disappear. What mistake am I making?

<div ng-app="DemoApp" ng-controller="DemoController">
  <ul>
    <li ng-repeat="image in temp">
      <img ng-src="{{image.src}}" />
    </li>
  </ul>
  <button class="btn btn-default" ng-click="loadMore()">LOAD MORE</button>
</div>
<script type="text/javascript">
        var DemoApp = angular.module("DemoApp", []);
        DemoApp.controller("DemoController",
              function DemoController($scope) {
                  $scope.quantity = 0;
                  $scope.temp = [];
                  $scope.loadMore = function () {
                      for (i = $scope.quantity; i <= $scope.quantity + 1; i++)
                      {
                          $scope.temp.push($scope.images[i]);
                      }
                      $scope.quantity = i;
                  }

                  $scope.images = [
                      { "src": "Images/1.jpg" },
                      { "src": "Images/2.jpg" },
                      { "src": "Images/3.jpg" },
                      ......
                      { "src": "Images/4.jpg" }
                  ];
              });
    </script>

The code works fine in a fiddle but not on my actual webpage.

http://jsfiddle.net/6cZ48/

Answer №1

Everything is functioning perfectly

Check out this amazing link

Awesome Demo

 var AppDemo = angular.module("AppDemo", []);
 AppDemo.controller("AppController",

 function AppController($scope) {
     $scope.amount = 0;
     $scope.data = [];
     $scope.showMore = function () {
         for (j = $scope.amount; j <= $scope.amount + 3; j++) {
             $scope.data.push($scope.items[j]);
         }
         $scope.amount = j + 1;
     }

     $scope.items = [{
         "url": "http://www.example.com/image1.png"
     }, {
         "url": "http://static.example.com/logo.png"
     }, {
         "url": "http://www.example.com/image1.png"
     }, {
         "url": "http://static.example.com/logo.png"
     }];
 });

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

Utilizing AngularJS's ng-repeat directive to iterate over an array stored in a $

My controller successfully retrieves and pushes an object onto an array using Parse: var mySite = angular.module('mySite', []); mySite.controller('listConnectors', ['$scope', function ($scope) { //Parse.initializ ...

Implementing a loading animation effect using AJAX that requires a loop delay and sleep functionality

My jquery ui dialog is loaded via ajax with a partial view from the server. Initially, the dialog opens as a dialog-ajax-loader and then animates/grows to fit the content once the call returns. The issue arises when the dialog content gets cached or the a ...

Is there a way to pass a v-modal as an array when setting Axios params?

I am currently dealing with a Vue Multiselect setup where the v-model is an array to accommodate multiple selected options. The challenge I am facing involves calling a route within the loadExtraOptions method and passing the array of IDs as a parameter ...

Does this Loop run synchronously?

Recently, I crafted this Loop to iterate through data retrieved from my CouchDB database. I am curious whether this Loop operates synchronously or if async/await is necessary for proper execution. database.view('test', 'getAllowedTracker&ap ...

Leveraging classes in routing with express framework

After deciding to convert the code from functions to classes, I ran into an issue where 'this' was undefined. Routing // router.js const ExampleController = require('./ExampleController'); const instanceOfExampleController = new Exam ...

The Angular service/value is failing to retrieve the updated variable from the $(document).ready() function

Currently, I'm facing an issue with my Angular service/value. It seems to be grabbing the original variable instead of the new one that is supposed to be inside $(document).ready(). Here's the relevant code snippet: var app = angular.module("app ...

Version 1.0 and higher of Ionic do not display the side menu when using tabs

I have developed an app using Ionic that includes a side menu with tabs feature. The side menu displays correctly when using Ionic version 0.9.27, but it does not show up when the version is 1.0 or higher. What could be causing this issue? HTML Layout & ...

Mastering the use of useMasterKey in Parse with Node.js: A guide

Greetings everyone, I have developed my app using parse.com but now I need to utilize the master key as some users have administrative roles. Initially, I attempted to use the JS SDK but was advised to switch to Cloud Code. So, I tried using Express and ...

What method does the framework use to determine the specific API being accessed?

How can the framework determine which API is being accessed? app.get('/user/:userId/name/export', function (req, res) { var userId = req.params.userId; } app.get('/user/:userId/name/:name', function (req, res) { var userId = req ...

Maintaining the footer and bottom section of the webpage

I am facing an issue with the footer on my website . I want the footer to always stay at the bottom of the page, even when I dynamically inject HTML code using JavaScript. The footer displays correctly on the main page , but it does not work as intended on ...

Developing a versatile directive in AngularJS

Creating a versatile directive in Angular is my current project. Consider this sample html: <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8> <title>Testing directives</title> <script src="../ ...

What causes the inconsistency in TypeScript's structure typing?

It is well-known that TypeScript applies structure typing, as demonstrated in the following example: interface Vector { x: number; y: number; } interface NamedVector { x: number; y: number; name: string; } function calculateLength(v: Vecto ...

Using AngularJS Scope to Map an Array within a JSON Array

I'm attempting to extract the type and url values from the media2 object within this JSON array and assign them to an AngularJS scope Array. "results":[ { "session2":[ { "__type":"Object", "abou ...

Issue with PHP $_GET function not functioning properly in conjunction with JQuery Mobile

I am currently developing an application using a combination of JQuery Mobile and PHP. The issue at hand is as follows: I am encountering difficulties when attempting to transfer values between different pages in my JQuery mobile app (for example, from #p ...

Understanding the $scope array and the use of $scope.$apply in AngularJS is

I'm currently diving into the world of AngularJs and encountering an issue that needs addressing. My goal is to display an array of items within a view. Initially, in the controller, I set up the array using $scope.arrName = [], then proceed to acqui ...

Delete any HTML content dynamically appended by AJAX requests

I'm currently working on a page dedicated to building orders, where the functionality is fully dependent on ajax for finding products and adding them dynamically. However, I encountered an issue when attempting to remove an item that was added via aj ...

There seems to be a hiccup in the JavaScript Console. It could potentially impact the functionality

Hey there, I'm encountering a strange issue in my IE 11 console. A JavaScript Console error has occurred. This may impact functionality. Whenever I try to run a for loop to create a list of elements within a ul tag 4000 times, the process stops at ...

What is the proper method for implementing respond_to with JSON data?

I find myself in a bit of a dilemma: I'm currently developing a centralized controller based on the guidance provided in this query and am encountering some confusion regarding the functionality of the respond_to method. In an API::Controller, my cod ...

Launching PDF on IE11 in a new tab seamlessly without any prompts - mssaveoropenblob

We are in the process of transitioning an ASP.NET MVC application that used to have the functionality of opening a PDF file in a new tab using FileContentResult. return new FileContentResult(byteArray, "application/pdf"); As part of this migration to Rea ...

I noticed that when using Next.js with the `revalidate: 1` option on a static page, it is triggering two full F5 refresh actions instead of just one. I was hoping for

Currently, I have set up a blog post edit page in my Next.js project. The post pages are utilizing the Incremental Static Regeneration feature with a revalidation time of 1 second for testing purposes. In the future, I plan to increase this to a revalidat ...