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

时间:2017-02-26 09:29:03

标签: angularjs view ng-controller ng-app

我必须在{{}}中显示来自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:dish未定义

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

      ....<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 个答案:

答案 0 :(得分:3)

您正在使用Controller作为。

转换你的

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

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

并通过ctrl

访问值 像这样

{{ctrl.dish.name}}

答案 1 :(得分:1)

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