Incorporating the outcomes obtained from an endless scrolling request into the default results showcased in AngularJS

In my code, I have a function that makes an initial $http call and displays the results. Following this function, there is another function that will make additional calls when the scroll event is triggered (using the ng-Infinite-Scroll module).

The issue I am facing is that I can't seem to append the results from these subsequent calls to the initial results displayed by the default call.

Below is a snippet of my code:

// Initial call
$scope.getDetails = function (id) {
  // First call
  $http.get('http://api.discogs.com/artists/' + id).
    success(function(data) {
        $scope.artist = data;
    });
    
  // Second call
  $http.get('http://api.discogs.com/artists/' + id + '/releases?page=1&per_page=12').
    success(function(data2) {
        $scope.releases = data2.releases;
    });  

  // Watching for changes in artist name
  $scope.$watch(function() {
    return $scope.artist;
  }, function() {
    var pos = $scope.artist.name.toLowerCase().indexOf(', the');
    if (pos != -1) {
      $scope.artist.name = 'The ' + $scope.artist.name.slice(0, pos);
    }
  });

  // Loading details 
  var _page = 1;
  $scope.releases = [];
  $scope.loadDetails = function() {
      _page++;
      console.log(_page);
      $http.get('http://api.discogs.com/artists/' + id + '/releases?page=' + _page + '&per_page=12').then(function(data2) {
          $scope.releases = data2.releases;
      });
  };    

  $scope.clicked = true;
  $scope.sliding = true;
}

After the initial call, the following function is triggered to fetch the first page with 12 items:

$http.get('http://api.discogs.com/artists/' + id + '/releases?page=1&per_page=12').
    success(function(data2) {
        $scope.releases = data2.releases;
}); 

However, when the loadDetails function is executed to fetch subsequent pages, the new results replace the existing ones instead of being appended. How can I correctly append the results instead of overwriting them?

If you have any hints or suggestions, please let me know!

Here's a working Plunker.

Answer №1

The main issue seems to be due to the absence of the id in the loadDetails signature, and the fact that .then was not properly waiting for $http.get to complete fetching the releases. I switched it with .success, and now it's functioning correctly.

You can see the updated version of your code on Plunker: http://plnkr.co/edit/gSwRNUn9mBvhWDtIaEzD

$scope.getDetails = function(id) {
  $scope._page = 1;
  $scope.releases = [];

  $http.get('http://api.discogs.com/artists/' + id).
  success(function(data) {
    $scope.artist = data;
  });
  $http.get('http://api.discogs.com/artists/' + id + '/releases?page=1&per_page=12').
  success(function(data2) {
    $scope.releases = data2.releases;
  });

  $scope.clicked = true;
  $scope.sliding = true;
};

$scope.loadDetails = function(id) {
  console.log(id);
  if (!angular.isUndefined(id)) {
    $scope._page++;
    console.log($scope._page);
    $http.get('http://api.discogs.com/artists/' + id + '/releases?page=' + $scope._page + '&per_page=12').
    success(function(data2) {
      for (var i = 0; i < data2.releases.length; i++) {
        $scope.releases.push(data2.releases[i]);
      }
    });
  }
};

I'm appending to the array using a loop and .push method.

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

Turn off the feature of map scrolling on OpenStreetMaps

Is there a way to prevent mouse interactions and scrolling in an open maps iframe? I have already tried adding the attribute scrollwheel="false" to no avail. Are there any CSS methods to achieve this? <iframe id= "mapsource" scrollwheel="false" src="ht ...

Executing PHP function through AJAX

I have thoroughly researched various resources regarding my issue but still have not been able to find a solution. My goal is to retrieve the result of a PHP function using jQuery AJAX. function fetch_select(){ val_name = $('#name').val(); ...

Aptana's Smart Code Completion

Does Aptana offer code completion for custom JavaScript libraries? If so, how can we enable it? ...

Using javascript and d3.js to display a set number of items on each line through printing

I'm currently working on a project that involves printing a specific number of rectangles per line using d3.js var rectangle = svgContainer.selectAll("rect").data(data).enter().append("rect") .attr("x", function(d,i){ return i*5}) ...

Display or conceal DIVs with multiple attribute values according to the selected value in a dropdown menu using the jQuery Filter Method

I am attempting to display or hide multiple services, each with a custom attribute called data-serviceregion, which may have multiple values based on the selected option from the dropdown. <form class="book-now"> <select name="region" id="region" ...

Refining/searching with selectors in AJAX response

As someone who is new to javascript and coding in general, I am facing a challenge with filtering and manipulating data received through an AJAX request. Unfortunately, I do not have access to the server-side code. The server responds with rota information ...

Peruse a spreadsheet for relevant information

I am currently facing an issue with a search bar that I implemented to filter through a table. However, for some reason, the filtering function does not seem to work on the tbody section of the table. The content in the tbody is generated dynamically usi ...

Exploring the syntax for navigating with JS Angular and HTML, such as when using the code snippet: ng-click="save(userForm)"

I am struggling to grasp the functionality of a specific part of this code. Despite my efforts to find tutorials, I have been unable to locate relevant information. There are two lines in particular that puzzle me: <button type="submit" class="btn btn ...

Parsing JSON data repeatedly using JavaScript within an HTML environment

The following code I found on a popular web development website works perfectly: <!DOCTYPE html> <html> <body> <h1>Customers</h1> <div id="id01"></div> <script> var xmlhttp = new XMLHttpRequest(); var url ...

Ways to include input values

In my form, there are 4 text boxes labeled as customer_phy_tot, customer_che_tot, and customer_bio_tot. I want to add up the values entered in these 3 boxes and display the sum in a 4th input box called customer_pcb_tot. customer_bio_obt.blur(function(){ ...

Collaborative use of AngularJS data models

In my Angular app, I have a controller called CanvasController that controls a canvas where users can add "widgets". The controller has an array named _currentWidgets, which holds object literals representing the widgets currently on the canvas. For examp ...

Adding styles dynamically using methods is not supported in Vue.js

When it comes to adding styles to an HTML element from methods, I ran into a small hiccup. Here's what I tried: <div class="add-profile-img" v-bind:style="getBackgroundImg()"> The method in question is: getBackgroundImg: function() { return { ...

Passing properties from the parent component to the child component in Vue3JS using TypeScript

Today marks my inaugural experience with VueJS, as we delve into a class project utilizing TypeScript. The task at hand is to transfer the attributes of the tabsData variable from the parent component (the view) to the child (the view component). Allow me ...

Utilize ES6 syntax to bring in a package as an extension of another package

To expand map projections in D3, it is recommended to import the necessary packages like so: const d3 = require("d3") require("d3-geo-projection")(d3) This allows you to access methods such as d3-geo-projection's geoAiry method fr ...

Using jQuery to pass multiple data-id attributes via AJAX

Having various tooltip links with distinct "data-id" attributes is a common scenario. <a href="" class id="tooltip" data-id="125">Link</a> <a href="" class id="tooltip" data-id="38">Link 2</a> In such instances, the "125" or "38" f ...

Simple steps to correct the npm installation of the React list filter

I encountered an issue while trying to install react-list-filter using npm (npm install react-list-filter). The error messages displayed in my console are as follows: npm ERR! code ETARGET npm ERR! notarget No matching version found for <a href="/cdn-c ...

How to capture two values in JavaScript input fields

I have developed a dynamic checker that instantly verifies user inputs against the database to see if they are already in use (e.g. username, phone number) Here is an example of the input box: <input type = "text" name = "user_username" placeholde ...

What could be the reason that Vue is not evaluating anything when the directive @click is specified as "true && method"?

Consider the following scenario: LandingPage.vue <template> <button @click="handleClick">Click Me</button> </template> <script> export default { methods: { handleClick() { this.$emit("click"); } } }; < ...

What methods can be used to accurately display the data type with TypeOf()?

When working with the following code: const data = Observable.from([{name: 'Alice', age: 25}, {name: 'Bob', age: 35}]); console.log(typeof(data)); The type is displayed as Object(). Is there a way to obtain more specific information? ...

Using jQuery UI Tabs to Dynamically Select a Tab Based on a Link

Recently, I have been exploring the idea of using a script to open a specific tab: $('.tofour').click(function() { // bind click event to link $tabs.tabs('select', 3); // switch to third tab return false; }); However, my dilem ...