0

I am new to angularjs, I have designed this code to show food items, but i dont know what the problem is...It gives nothing as output. I have tried almost everything, but I cant seem to find 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">
        <div class="col-xs-12">
                    <div class="media-left media-middle">
                        <a href="">
                            <img ng-src={{dish_cmnt.image}} alt="Uthapizza">
                        </a>
                        <div class="media-body">
                            <h2 class="media-heading">{{dish_cmnt.name}}
                        <span class="label label-danger">{{dish_cmnt.label}}</span>
                        <span class="badge">{{dish_cmnt.price | currency}}</span></h2>
                        <p>{{dish_cmnt.description}}</p>
                        </div>
                    </div>
        </div>
        <div class="col-xs-9 col-xs-offset-1">

        </div>
    </div>

</div>

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

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

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

        var dish_cmnt={
                      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>
4
  • 2
    I can't see ng-app="confusionApp" anywhere Commented Apr 7, 2017 at 19:37
  • your app has multiple issues, but the one that stands out is that you are defining your dish_cmnt as a simple var, which will never be accessible to the HTML. It either needs to be a property of $scope, or it needs to be a property of your controller. You might want to study some more angular examples. Commented Apr 7, 2017 at 19:39
  • And even when you added ngApp, it will still won't work because you're not declaring your controller using ng-controller="dishDetailController as vm" (And then - {{vm.dish_cmnt.name}}, etc..) Commented Apr 7, 2017 at 19:39
  • also, this.dish = dish would throw an error, since dish isn't defined anywhere. Commented Apr 7, 2017 at 19:42

1 Answer 1

1

Firstly, you were missing ng-app="confusionApp" so I added that.

Next, your template didn't have a reference to dishDetailController so did that using ng-controller="dishDetailController as ctrl" and using ctrl.dish_cmnt instead of direct dish_cmnt.

Next, this.dish = dish was throwing error since dish was undefined. Changed that to this.dish_cmnt = dish_cmnt since we needed reference to dish_cmnt anyway.

That fixed it.

Here's your working code snippet.

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

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

  var dish_cmnt = {
    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_cmnt = dish_cmnt;

});
<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 -->
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
</head>

<body ng-app="confusionApp">

  <div class="container">
    <div class="row row-content" ng-controller="dishDetailController as ctrl">
      <div class="col-xs-12">
        <div class="media-left media-middle">
          <a href="">
            <img ng-src={{ctrl.dish_cmnt.image}} alt="Uthapizza">
          </a>
          <div class="media-body">
            <h2 class="media-heading">{{ctrl.dish_cmnt.name}}
              <span class="label label-danger">{{ctrl.dish_cmnt.label}}</span>
              <span class="badge">{{ctrl.dish_cmnt.price | currency}}</span></h2>
            <p>{{ctrl.dish_cmnt.description}}</p>
          </div>
        </div>
      </div>
      <div class="col-xs-9 col-xs-offset-1">

      </div>
    </div>

  </div>


</body>

</html>

Sign up to request clarification or add additional context in comments.

3 Comments

I tried this but it does not show the content but {{ctrl..dish_cmnt.name}} and all others like this
@GiteshSawariya what ever you have after ctrl should be defined in your controller. So here, if you want to show ctrl.name, you gotta have this.name = "gitesh" in your controller somewhere
@GiteshSawariya there might be some other issue then. you can maybe create a fiddle/plunker to reproduce the issue and share it here?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.