AngularJS - значения объекта не отображаются в {{}}

Мне нужно показать некоторые значения из Json в {{}}, но я вижу, что значения просто отображаются в консоли и прямо в app.controller. Просто отключается этот app.controller, а значения не отображаются. Вот некоторые части кода:

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

И отделка:

  ...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 ("Эй, эй, эй!");// Это отлично видно в консоли console.log(dish.name);// но это показывает... ReferenceError: блюдо не определено

И на виде это тоже не работает... Не отображается, просто пусто.

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

NgApp находится в теге html:

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

И ngController помещается в div, который содержит весь вид:

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

Так... что не так?

2 ответа

Решение

Использовать контроллер, как в HTML, как это

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

Вы используете контроллер как.

Просто конвертируй свой

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

в

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

и значение доступа через ctrl

как это

{{ctrl.dish.name}}
Другие вопросы по тегам