3

I am a beginner in AngularJS and I am currently around with the basics. I am trying to create a simple menu but I have a couple of Angular expressions that aren't evaluating; {{ dish.description }} and {{ dish.comments }}. I tried to debug the page on Chrome's developer console but no errors were raised so I am not sure what could be the problem.

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

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head
         content must come *after* these tags -->
    <title>Ristorante Con Fusion: Menu</title>
        <!-- Bootstrap -->
    <link href="../bower_components/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="../bower_components/bootstrap/dist/css/bootstrap-theme.min.css" rel="stylesheet">
    <link href="../bower_components/font-awesome/css/font-awesome.min.css" rel="stylesheet">
    <link href="styles/bootstrap-social.css" rel="stylesheet">
    <link href="styles/mystyles.css" rel="stylesheet">

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>

<body>

    <div class="container">
        <div class="row row-content" ng-controller="dishDetailController as dishCtrl">
          <div class="media" ng-repeat="dishes in dishCtrl.dish">
            <div class="col-xs-12">
              <p> {{ dishes.description }} </p>
            </div>
            <div class="col-xs-9 col-xs-offset-1">
              <p> {{ dishes.comments }} </p>
            </div>
          </div>
        </div>

    </div>

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

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

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

            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"
                               }

                           ]
                    };

            this.dish = dish;

        });

    </script>

</body>

</html>
Pankaj Parkar
  • 134,766
  • 23
  • 234
  • 299
mesllo
  • 545
  • 7
  • 29
  • Please make sure first that your controller has been called or not. You can use alert or console.log() to verify for that. – Gurpinder Apr 06 '16 at 10:43

2 Answers2

2

dish should be an array if you wanted to do ng-repeat over it.

var dish=[{...object1..}, {..object2... ]};
Pankaj Parkar
  • 134,766
  • 23
  • 234
  • 299
-1
  1. First of all you need to assign dish as a $scope variable - $scope.dish. Angular provides scope to allow your controller variable to bind and can be access in HTML.
  2. ng-repeat use to iterate $scope variable in DOM so fr this instead of dish={name:'Uthapizza',...} it has to be something like dish=[object1, object2]; For Example

    $scope.dish=[{description:'description 2',comments:'comment 1'}, {description:'description 2',comments:'comment 2'}]

    1. you may get injector error for this you have to inject $scope in controller.
    2. you don't need to use this.dish = dish; in angularJS. Also attached the solution.

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

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

                $scope.dish=[{
                                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"
                             }
                           ];
            });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.3/angular.min.js"></script>
<div class="container" ng-app="confusionApp">
  <div class="row row-content" ng-controller="dishDetailController as dishCtrl">
    <div class="media" ng-repeat="dishes in dish">
      <div class="col-xs-12">
        <p> {{ dishes.author }} </p>
      </div>
      <div class="col-xs-9 col-xs-offset-1">
        <p> {{ dishes.comment }} </p>
      </div>
      <div class="col-xs-9 col-xs-offset-1">
        <p> {{ dishes.rating }} </p>
      </div>
    </div>
  </div>

</div>