Difficulty in accessing controller data in AngularJS with ng-repeat

I am trying to display comments using ng-repeat in a section, but I am having trouble accessing the data.

Even after debugging, I cannot access the data without modifying the controller. I am new to Angular and prone to making mistakes.

HTML / JS

'use strict';

angular.module('commentsApp', [])       
        .controller('DishDetailController', ['$scope', function($scope) {

            var dish={
                          name:'Uthapizza',
                          image: 'images/uthapizza.png',
                          category: 'mains',
                          label:'Hot',
                          price:'4.99',
                          description:'A unique combination of Indian Uthappam (pancake) and Italian pizza, topped with Cerignola olives, ripe vine cherry tomatoes, Vidalia onion, Guntur chillies and Buffalo Paneer.',
                          comments: [
                               {
                                   rating:5,
                                   comment:"Imagine all the eatables, living in conFusion!",
                                   author:"John Lemon",
                                   date:"2012-10-16T17:57:28.556094Z"
                               },
                               {
                                   rating:4,
                                   comment:"Sends anyone to heaven, I wish I could get my mother-in-law to eat it!",
                                   author:"Paul McVites",
                                   date:"2014-09-05T17:57:28.556094Z"
                               },
                               {
                                   rating:3,
                                   comment:"Eat it, just eat it!",
                                   author:"Michael Jaikishan",
                                   date:"2015-02-13T17:57:28.556094Z"
                               },
                               {
                                   rating:4,
                                   comment:"Ultimate, Reaching for the stars!",
                                   author:"Ringo Starry",
                                   date:"2013-12-02T17:57:28.556094Z"
                               },
                               {
                                   rating:2,
                                   comment:"It's your birthday, we're gonna party!",
                                   author:"25 Cent",
                                   date:"2011-12-02T17:57:28.556094Z"
                               }

                           ]
                    };

        $scope.dish = dish;

    }]);
<div ng-app="commentsApp">
  <div ng-controller="DishDetailController">
    <div class="col-xs-9 col-xs-offset-1">
      <div class="blockquote" ng-repeat="entry in dish.comments">
        <blockquote>
          <p>{{entry.rating}} Stars</p>
          <p>{{entry.comment}}</p>
          <footer>{{entry.author}}, {{entry.date | date}}</footer>
        </blockquote>
      </div>
    </div>
  </div>
</div>

Answer №1

The error you made here is that the entry object represents an object in the dish.comments array. To access data, you simply need to remove the comments part.

<div ng-app="commentsApp">
  <div ng-controller="DishDetailController">
    <div class="col-xs-9 col-xs-offset-1">
      <div class="blockquote" ng-repeat="entry in dish.comments">
        <blockquote>
          <p>{{entry.rating}} Stars</p>
          <p>{{entry.comment}}</p>
          <footer>{{entry.author}}, {{entry.date | date}}</footer>
        </blockquote>
      </div>
    </div>
  </div>
</div>

Answer №2

give this a shot:

 <div ng-app="commentsApp">
  <div ng-controller="DishDetailController">
    <div class="col-xs-9 col-xs-offset-1">
      <div class="blockquote" ng-repeat="entry in dish.comments">
        <blockquote>
          <p>{{entry.rating}} Stars</p>
          <p>{{entry.comment}}</p>
          <footer>{{entry.author}}, {{entry.date | date}}</footer>
        </blockquote>
      </div>
    </div>
  </div>
</div>

Answer №3

To implement the necessary changes, update your code with the following:

<div ng-app="commentsApp">
<div ng-controller="DishDetailController">
   <div class="col-xs-9 col-xs-offset-1">

    <div class="blockquote" ng-repeat="entry in dish.comments">
      <blockquote>
      {{entry}}
      <p>{{entry.rating}} Stars</p>
      <p>{{entry.comments}}</p>
      <footer>{{entry.author}}, {{entry.date | date}}</footer>
    </blockquote>
  </div>
</div>
</div>

For a live example, visit the following plunker link: https://plnkr.co/edit/enMspBPd1y5duq6szCrX

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

Jade not binding correctly with Angular.ErrorMessage: Angular bindings are

Struggling with simple binding in Angular and Jade. I've tried moving JavaScript references to the end of the document based on advice from previous answers, but still no luck. Any ideas on what might be wrong? File: angular.jade extends layout blo ...

Are Global variables supported in the EXT JS framework?

When working with Java and C++, it is common practice to store a variable globally so that its value can be accessed from anywhere within the project. For example, if I am working inside a class named Residence and saving an INT as the residenceNumber to a ...

Troubleshooting: Why the TableTools basic usage example is not functioning as

After replicating the code from http://datatables.net/release-datatables/extensions/TableTools/examples/simple.html in my Visual Studio project, I organized the files by saving two css files as style1.css and style2.css, along with three js files named sc ...

Can JavaScript executed within a web browser have the capability to manipulate files on the underlying host system's file system?

Is it possible to programmatically move files or folders from a website using code? I am aware that with Python and Node.js, this can be achieved through the OS, path, and fs modules. Can JavaScript running in a website also handle file management tasks ...

Navigating a JSON message received from a websocket: Best practices

How can I cycle through various types of JSON messages received from WebSocket and trigger a function based on the type to modify observables in MobX? Can anyone assist with this? For instance, one of the simple messages looks like this: { name: "as ...

In my Next.js project, I am looking for a way to make a modal continue to stay

I am looking to implement a feature where the modal remains visible even after a website refresh. Currently, when a user logs out, the page refreshes and the modal displaying "sign-out successful" disappears. My goal is to have the modal reappear after t ...

Issue with retrieving the ID of a dynamically created element with jQuery

Whenever I try to execute my function to display the id for a click event of a tag that has items appended dynamically, the alert does not show the id. Instead, it displays 'undefined'. Can anyone help me figure out where I am going wrong? Here ...

Karma is reporting an error with TypeScript, saying it cannot locate the variable 'exports'

Currently, I am in the process of mastering how to write Unit Test cases for an angular project coded in Typescript. To facilitate this, I have opted for utilizing Karma and Mocha. Below lays out the structure of the application: Project/ ├── app/ ...

Tips for modifying CSS when a user scrolls beyond a specific div

Currently, I am working on implementing a scroll function that dynamically moves elements based on the user's scrolling behavior. The code I have written works to some extent and successfully moves the elements. However, my goal is to create a list o ...

Linking chained functions for reuse of code in react-redux through mapStateToProps and mapDispatchToProps

Imagine I have two connected Redux components. The first component is a simple todo loading and display container, with functions passed to connect(): mapStateToProps reads todos from the Redux state, and mapDispatchToProps requests the latest list of todo ...

Is it possible to alter the video dynamically according to the state in Vuex?

I am working on a small web application that mimics the appearance of FaceTime. My goal is to switch between video elements by clicking a "Next" button, which will update a value in Vuex and swap out the video accordingly. I initially attempted this appr ...

Use both a PayPal form and a PHP form simultaneously by implementing a single button that can submit both forms with the help of jQuery and AJAX

While I have a good grasp on HTML and PHP, my knowledge of jQuery and Ajax is quite limited. After spending a significant amount of time searching for answers here: One form, One submission button, but TWO actions and here : Writing to my DB and submittin ...

The error occurred in Commands.ts for Cypress, stating that the argument '"login"' cannot be assigned to the parameter of type 'keyof Chainable<any>))`

Attempting to simplify repetitive actions by utilizing commands.ts, such as requesting email and password. However, upon trying to implement this, I encounter an error for the login (Argument of type '"login"' is not assignable to parameter of t ...

Should one consider using the Express app.use() method within an asynchronous callback function?

Seeking advice on a recommended best practice. I recently developed an Express server that generates a JWT and sends it to the client whenever they hit an API endpoint. To maintain modularity, I separated the route handler into its own module and exporte ...

The Vue-router is constantly adding a # symbol to the current routes, and it's important to note that this is not the typical problem of hash and

After setting up my router file using Vue 3, it looks like this: import { createRouter, createWebHistory } from "vue-router"; import Home from "../views/Home.vue"; const routes = [ { path: "/", name: &quo ...

What is causing the search feature to malfunction on the Detail page?

In my project, I have different components for handling shows. The Shows.jsx component is responsible for rendering all the shows, while the ProductDetails component displays information about a single show. Additionally, there is a Search component which ...

AngularJS is throwing an error of "TypeError: Cannot read property 'then' of undefined."

Within an AngularJS project, I am aiming to retrieve user data from those who sign in and utilize this data within an HTML form. To achieve this, I have a script called sessionService.js that sends requests to the server and receives responses. The follow ...

Navigate to a different HTML file following a JavaScript function in Ionic and AngularJS with Cordova

I am currently creating an Android application in Cordova Tools for Visual Studio using Ionic and AngularJS. My goal is to redirect to another HTML page once my function has completed its execution, but I'm having trouble getting it to work correctly ...

Creating dynamic DIV elements in an AngularJS file using data retrieved from a Sql server

I need to dynamically add elements based on data retrieved from a MSSQL server in the specified area: <div id="ApplicationForm" class="tabcontent" style="display:none;"> <div class="tab_section"> ...

Ways to showcase corresponding information for an item contained within an array?

I'm working with a function that is designed to retrieve specific descriptions for objects nested within an array. The purpose of the function (findSettings()) is to take in an array (systemSettings) and a key (tab12) as arguments, then use a switch s ...