Beginning AngularJS by implementing the ng-controller directive

I'm facing an issue while attempting to create a simple exercise. It seems like I may not be calling the controller variable correctly, which is causing me to be stuck. How can I troubleshoot and fix this code? The output page is displaying: {{dish.name}} {{dish.label}} {{dish.price | currency}}

<html lang="en" ng-app="a">

<head>.... </head>

<body>

    <div class="container">
        <div class="row row-content" ng-controller="dishDetailController as dishDC">
            <div class="col-xs-12">
                <p>Insert the dish details here</p>
                <div class="media-body">
                  <ul class="media-list">
                    <li class="media" ng-repeat="dish in dishDC.dishes">
                    <div class="media-left media-middle">
                        <a href="#">
                        <img class="media-object img-thumbnail"
                         ng-src={{dish.image}} alt="a">
                        </a>
                    </div>
                    <div class="media-body">
                        <h2 class="media-heading">{{dish.name}}
                         <span class="label label-danger">{{dish.label}}</span>
                         <span class="badge">{{dish.price | currency}}</span></h2>
                        <p>{{dish.description}}</p>
                    </div>
                    </li>
                  </ul>
                </div>
            </div>
            <div class="col-xs-9 col-xs-offset-1">
                <p>Add comments here</p>
            </div>
        </div>

    </div>

    <script src="../bower_components/angular/angular.min.js"></script>
    <script>

        var app = angular.module('confusionApp',[]);

        app.controller('dishDetailController', function() {

            var dishes={
                          name:'a',
                          image: 'images/a.png',
                          category: 'a',
                          label:'a',
                          price:'7.88',
                          description:'a',

                    };

            this.dishes = dishes;

        });

    </script>

</body>

</html>

Answer №1

Make sure to pass an array to ng-repeat for iteration. If you are passing an object, it will not work as expected. Convert the object into an array of objects and everything should work fine.

<div class="container">
    <div class="row row-content" ng-controller="dishDetailController as dishDC">
        <div class="col-xs-12">
            <p>Include all the dish details in this section</p>
            <div class="media-body">
              <ul class="media-list">
                <li class="media" ng-repeat="dish in dishDC.dishes">
                <div class="media-left media-middle">
                    <a href="#">
                    <img class="media-object img-thumbnail"
                     ng-src={{dish.image}} alt="a">
                    </a>
                </div>
                {{dish.name}}
                <div class="media-body">
                    <h2 class="media-heading">{{dish.name}}
                     <span class="label label-danger">{{dish.label}}</span>
                     <span class="badge">{{dish.price | currency}}</span></h2>
                    <p>{{dish.description}}</p>
                </div>
                </li>
              </ul>
            </div>
        </div>
        <div class="col-xs-9 col-xs-offset-1">
            <p>Add comments here</p>
        </div>
    </div>

</div>

<script src="angular.min.js"></script>
<script>

    var app = angular.module('confusionApp',[]);

    app.controller('dishDetailController', function() {

        this.dishes=[{
                      name:'James',
                      image: 'images/James.png',
                      category: 'HouseHold',
                      label:'Lab',
                      price:'7.88',
                      description:'Desc',

                }];



    });

</script>

Answer №2

Perhaps the reason for this issue is that the ng-app directive has not been properly defined for the application.

To resolve this, consider including confusionApp in your ng-app like so:

 <html lang="en" ng-app="confusionApp">

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

Combining two sets of data into one powerful tool: ngx-charts for Angular 2

After successfully creating a component chart using ngx-charts in angular 2 and pulling data from data.ts, I am now looking to reuse the same component to display a second chart with a different data set (data2.ts). Is this even possible? Can someone guide ...

Troubleshooting: Angular post request encountering 415 error issue

I'm attempting to retrieve the response from the code snippet below, which produces a JSON output. When I try this on the server, I encounter a 415 error and do not receive the response. constructor(private http: HttpClient){} public getReports(pos ...

Making a call to Ajax while specifying the contentType as 'application/json'

After some troubleshooting, I discovered that removing the content-Type works fine. However, the jsonitem received on the PHP side is showing syntax errors. From my research, it seems like specifying the content type when sending Json objects is crucial. ...

Issues with reading response headers in AngularJS when using Apiary.IO?

I am attempting to mock my API using Apiary.io, but I am facing an issue where I cannot retrieve any headers from the response object in angularJS. I have verified that at least Content-Type: application/json is correctly set up by checking in firebug. The ...

Guide on how to navigate to a different page while making a request in ASP.NET

I have a standard asp.net page containing multiple buttons. I would like to add one button labeled Stop Request, which, when clicked, refreshes the page regardless of which other button the user has clicked previously. Here is an example of how I envision ...

What could be causing the timeout issue when trying to fetch guild members?

Hey there! I need some help with my discord bot. I'm trying to get a list of members ordered by their ids, but when I run the code below, all it does is log "Couldn't fetch members" without any other errors showing up. After digging deeper, I re ...

By pairing delay(0) with refCount(), we can achieve optimal efficiency

The refCount operator is discussed in this article. It explains the necessity of adding delay(0) to prevent unsubscription of observable A: import { Observable } from "rxjs/Observable"; const source = Observable.defer(() => Observable. ...

Transmitting Cookie from JavaScript to PHP

My task involves sending a JavaScript string to my server. I have managed to create a cookie for this purpose, and now I need it to perform a specific action (like a simple echo). Below is the button function I am using with Bokeh: const data = source.d ...

Sending an Array of Data to Jquery

One of the buttons in my code looks like this: <button id='abc' value=['fred', 26]></button> I am looking for a way to retrieve the two values using JQuery's .val() function. My attempt $("#abc").val()[0] only return ...

Guide to creating an inline form that spans the entire width below the navbar logo within the "navbar" component

I'm new to working with bootstrap and trying to create an inline form in the navbar next to the navbar-brand. However, as I resize the window, the form ends up below the navbar-brand link and doesn't align properly, making it look awkward. Can so ...

Issue with updating state using useCallBack in React when handling resize events

I'm a bit confused about how to properly use the useCallback hook. I have a hook that detects whether I'm on desktop, tablet, or mobile based on the screen width. This part is working fine. However, I'm running into an issue in a specific c ...

Executing a Mysql callback inside another callback in Node.js

Hi, I've hit a roadblock with my first callback function "selectArticleByTitle(title, callback)". The terminal keeps throwing the error "Cannot read property 'id' of undefined". I'm unsure how to properly complete the first callback so ...

DataTables.js, the powerful JavaScript library for creating interactive tables, and its compatible counterpart

I'm currently making some changes to a dynamic table that require enabling a vertical scroll bar. As a result, I need to implement this vertical scroll bar mechanism on an existing table. The code in our project utilizes dataTables.js version 1.5.2 ...

A guide on organizing JSX elements based on JSON data

What I Aim to Achieve: I am looking to display a list of elements using the .map method, and then arrange them in descending order based on their date. Despite attempting to use .sort and .filter before rendering the elements, I have not been successful. ...

Utilize a JavaScript variable within HTML within the confines of an if statement

On my HTML page, I am dynamically displaying a list of properties and then counting how many are displayed in each div. <script type="text/javascript> var numItems = $('.countdiv').length; </script> The class name for each property ...

How can I make the columns in Next.js / Tailwind expand horizontally first instead of vertically?

Recently, I decided to customize the Next.js Image Gallery starter for some hands-on experience in full stack development with Next.js. My goal was to create a chronological photo gallery of a recent trip, but encountered an issue where the responsive colu ...

Tips on transitioning a Node.js application from JavaScript to TypeScript incrementally

I have a JavaScript node application that has grown quite large and I am considering migrating to TypeScript for faster development and easier code maintenance. I have installed TypeScript along with the node and mocha types using the following commands: ...

Uncertainty surrounding the concept of JavaScript objects

What is the reason for this not functioning properly? function displayValue(a,b){ this.a = a; this.b = b; $('p').text(a + " " + b); } var display = new displayValue(); display('1','2'); How does this still work ...

What is the best way to contact all products that have been made by a specific User?

Using MongoDB with Mongoose, I have successfully implemented basic CRUD operations. However, I am facing difficulty in fetching all the products created by a specific user. I'm unsure if my approach is correct, but I attempted to use this syntax: user ...

The AJAX comments triggered an Uncaught SyntaxError due to an unexpected identifier

Can someone assist me in identifying the issue? I keep receiving an Unexpected identifier error when attempting to use ajax for sending comments. Code: function funcSuccess(data) { $("#comment_ajax").text(data); } function funcBefore() { $("#comme ...