angularjsviewng-controllerng-app

AngularJS - object values not show in {{}}


I have to show some values from a Json in the {{}} but I see the values just are shown in console and right inside app.controller. Its just going off this app.controller and the values are not shown. These are some parts of the code:

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....

And finishing:

  ...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;
  console.log(dish["name"]); console.log(dish.image); console.log(dish.category);
  console.log(dish.label); console.log(dish.price); console.log(dish.description);
  console.log("----------------------------------------------------------");
  console.log(dish.comments[0]["rating"]); console.log(dish.comments[0]["comment"]);
  console.log(dish.comments[0]["author"]); console.log(dish.comments[0]["date"]);
});

console.log("Hey hey hey!");//This shown perfectly in console console.log(dish.name);//but this is showing... ReferenceError: dish is not defined

And on the view it is not working too...Not is displayed, just blank.

      ....<img class="media-object img-thumbnail"
      ng-src={{image}} alt="Uthappizza">
    </a>
  </div>
  <div class="media-body">
    <h2 class="media-heading">{{dish.name}}
     <span class="label label-danger">{{dish.label}}</span>....

The ngApp is in html tag:

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

And ngController is placed in a div that holds the entire view:

<div class="container" ng-controller="dishDetailController">

So...what's wrong?


Solution

  • use controller as in the html like this

    <div ng-app="confusionApp" ng-controller="dishDetailController as vm">
    <img class="media-object img-thumbnail"
          ng-src={{vm.image}} alt="Uthappizza">  
      <div class="media-body">
        <h2 class="media-heading">{{vm.dish.name}}
         <span class="label label-danger">{{vm.dish.label}}</span>
    </div>
    

    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:2,
            comment:"It's your birthday, we're gonna party!",
            author:"25 Cent",
            date:"2011-12-02T17:57:28.556094Z"
          }
    
        ]
      };
    
      this.dish = dish;
      console.log(dish["name"]); console.log(dish.image); console.log(dish.category);
      console.log(dish.label); console.log(dish.price); console.log(dish.description);
      console.log("----------------------------------------------------------");
      console.log(dish.comments[0]["rating"]); console.log(dish.comments[0]["comment"]);
      console.log(dish.comments[0]["author"]); console.log(dish.comments[0]["date"]);
    });
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
    <div ng-app="confusionApp" ng-controller="dishDetailController as vm">
    <img class="media-object img-thumbnail"
          ng-src={{vm.image}} alt="Uthappizza">  
      <div class="media-body">
        <h2 class="media-heading">{{vm.dish.name}}
         <span class="label label-danger">{{vm.dish.label}}</span>
    </div>