AngularJS - 对象值未显示在 {{}} 中

AngularJS - object values not show in {{}}

我必须在 {{}} 中显示来自 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("Hey hey hey!");//这在控制台中完美显示 console.log(dish.name);//但这显示... ReferenceError: dish is not defined

并且在视图中它也不起作用...不显示,只是空白。

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

所以...怎么了?

您正在使用 Controller 作为。

只需转换您的

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

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

并通过 ctrl

访问值

像这样

{{ctrl.dish.name}}

像这样使用 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>