My data is not appearing with ng-repeat or ng-bind

One issue I am encountering is that my ng-repeat / ng-bind is not displaying the data within $scope.articles, even though I am getting the expected data in the console. To help identify the problem more easily, I have created a code snippet below:

var App = angular.module('App', []);
App.controller('WebCtrl', function($scope, $http) {

  $scope.start = [{
    "id": 1,
    "text": "test1"
  }, {
    "id": 2,
    "text": "test2"
  }, {
    "id": 3,
    "text": "test3"
  }, {
    "id": 4,
    "text": "test4"
  }];
  $scope.articles = $scope.start;

  $http.get('/')
    .then(function() {
      $scope.menu = function(id) {
        $scope.articles = $scope.start[id];
        console.log($scope.articles);
      }
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<!doctype html>
<html ng-app="App">

<head>
  <meta charset="utf-8">
  <title>Todos $http</title>
  <script src="app.js"></script>
</head>

<body ng-controller="WebCtrl">
  <ul>
    <li style="list-style: none">
      <button ng-click="menu(0)">1</button>
      <button ng-click="menu(1)">2</button>
      <button ng-click="menu(2)">3</button>
      <button ng-click="menu(3)">4</button>
    </li>
  </ul>

  <ul>
    <li style="list-style: none" ng-repeat="article in articles">
      {{article.text}}
    </li>
  </ul>
</body>

</html>

Answer №1

When looking at your code, I see that it begins with an articles object that points to an array. As you go inside the menu, it transforms into an object, and ng-repeat loops through its keys.

I believe the key change lies in:

$scope.articles = $scope.start.slice(id, id + 1);

var App = angular.module('App', []);
App.controller('WebCtrl', function($scope, $http) {

  $scope.start = [{
    "id": 1,
    "text": "test1"
  }, {
    "id": 2,
    "text": "test2"
  }, {
    "id": 3,
    "text": "test3"
  }, {
    "id": 4,
    "text": "test4"
  }];
  $scope.articles = $scope.start;

      $scope.menu = function(id) {
        $scope.articles = $scope.start.slice(id, id + 1);
        console.log($scope.articles);
      }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<!doctype html>
<html ng-app="App">

<head>
  <meta charset="utf-8">
  <title>Todos $http</title>
  <script src="app.js"></script>
</head>

<body ng-controller="WebCtrl">
  <ul>
    <li style="list-style: none">
      <button ng-click="menu(0)">1</button>
      <button ng-click="menu(1)">2</button>
      <button ng-click="menu(2)">3</button>
      <button ng-click="menu(3)">4</button>
    </li>
  </ul>

  <ul>
    <li style="list-style: none" ng-repeat="article in articles">
      {{article.text}}
    </li>
  </ul>
</body>

</html>

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

Generating a bullet list from an array of objects

Looking to create an unordered list with vanilla JS, using an array of objects to populate the list. Struggling a bit on how to accomplish this. Here is my current code: let myObj = [ {name: "Harry Potter", author: "JK Rowling"}, {name: "Hunger Gam ...

Observing changes in Angular.js components when binding values from services to HTML

Is there a way to bind the changing HTML value from a service in my component? The value from the service is dynamic, but my component does not observe it. I simply want to display this variable as text in my component's HTML. Here is an example setu ...

Angular JS (1.5) is experiencing difficulties with successfully processing an HTTP POST request

I've been working on implementing HTTP post requests in AngularJS (1.5). Initially, I pass the request data to a factory method, then trigger the HTTP post request and send the response back to the controller. However, I keep encountering the followi ...

Generating an in-page anchor using CKeditor 5

We are currently integrating CKEditor 5 into one of our projects. Our goal is to enable the end-user to generate an in-page anchor tag that can be accessed through other links (e.g., <a name='internalheading'> which can be navigated to via ...

I am encountering an issue with my authentication system where it is returning

I'm currently experiencing an issue with my authentication system using passport. For some reason, I keep getting an 'undefined' value returned. Can anyone provide assistance? Here is the code snippet in question: app.js passport.use(new L ...

jQuery - restrict input field based on the value of a different selected field

Could anyone recommend a jQuery plugin that can achieve the following functionality? For example: <label><input type="checkbox" depends_on="foo=5" name="boo" ... /> Check </label> <select name="foo" ... > <option value="5" se ...

Tips on changing the color of a dropdown select option in Angular 8

I have been experimenting with changing the color of a dropdown select menu based on the value selected by the user. Here is the code I have been working with: App.component.ts @Component({ selector: 'my-app', templateUrl: './app.comp ...

Organize items within an array based on dual properties rather than a single one

Here is an array of objects that I would like to group based on certain keys (JSON format): [ { "name": "john", "lastName": "doe", "gender": "male" }, { "name": &qu ...

Execute a multer request to upload an image (using javascript and express)

I am utilizing the Express server as an API gateway for my microservices, specifically to process and store images in a database. The issue at hand is that within the Express server, I need to forward an image received through a POST request from the clien ...

JSON object fails to iterate with ng-repeat

It must be the scorching temperature... Having a json object that I'm eager to loop through using ng-repeat, it should be straightforward, but alas! it's just not cooperating. Here's the HTML snippet: <a data-ng-repeat="x in template.m ...

Measuring Page Loading Status using Ajax

I'm still learning Ajax and JQuery, and I've been having a tough time putting this together. My goal is to use ajax navigation to load URLs and implement back and front navigations with popstate. The code below is functional, but I'm facing ...

Generating intricate JSON structures with JavaScript programming instructions

In the world of JSON, I am still a novice. I need to use JavaScript to construct the JSON structure below, but I'm struggling with adding the second element ("12101") and populating the people in the JSON Structure. Below is the code I tried, however, ...

AngularJS: When the expression is evaluated, it is showing up as the literal {{expression}} text instead of

Resolved: With the help of @Sajeetharan, it was pinpointed that the function GenerateRef was faulty and causing the issue. Although this is a common query, I have not had success in resolving my problem with displaying {{}} to show the outcome. I am atte ...

The display message in Javascript after replacing a specific string with a variable

I'm currently working on a task that involves extracting numbers from a text input, specifically the first section after splitting it. To test this functionality, I want to display the result using an alert. The text input provided is expected to be ...

Guide on submitting a form via Ajax on a mobile app

Looking for a way to submit the form located in components/com_users/views/login/tmpl/default_login.php using Ajax. <form action="<?php echo JRoute::_('index.php?option=com_users&task=user.login'); ?>" method="post"> <fie ...

Prevent Object Prop Modification in Vue.js

Within the parent component, I initially have an empty filter object like this: {}. The child component, called filter component, is a child of the parent component and here's how it is implemented: <filter-component :filters.sync="filters&q ...

Tips for transforming numerical date data into a string format

Is there a method to convert the numeric month in a table into a string format? <table style="width: 100%;"> <tr> <th>Date</th> <th>Total</th> </tr> <tr> <td id="date ...

What is the best way to incorporate tinymce into webpack?

I am facing an issue with integrating tinymce with webpack. It assigns a property called tinymce to window, so one solution is to use the following syntax to require() it (as explained in the bottom of the EXPORTING section of the webpack documentation): ...

What is the method to select a hyperlink that includes a variable in the "href" attribute and click on it?

Currently, I am in the process of creating acceptance tests utilizing Selenium and WebdriverIO. However, I have encountered a problem where I am unable to successfully click on a specific link. client.click('a[href=#admin/'+ transactionId + &apo ...

Is it possible to extract around 10 variables from a JavaScript code, then display them on a webpage after execution?

I have just completed writing a Javascript code with around 3,000 lines. This code contains over 60 variables, but there are a few specific variables that I would like to display on my main HTML page. These variables include: totalTime longitudinalAcceler ...