lisarko8077 lisarko8077 - 1 year ago 108
AngularJS Question

add an object declared on the controller in Angular JS

i am an angularJS beginner and i have an issue.
I want to add data to a media-object in html using an object declared in the script inside the controller, but i can't find which directive i must use.

Here is the code:

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

<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=""></script>
<script src=""></script>


<div class="container">
<div class="row row-content" ng-controller="dishDetailController as dishCtrl">
<div class="col-xs-12">
<div class="media">
<div class="media-left media-middle">
<a href="#">
<img class="media-object img-thumbnail" ng-src={{dish.image}} alt="Uthapizza">
<div class="media-body">
<h4 class="media-heading">{{}}<span class="label label-danger">{{dish.label}}</span><span class="badge">{{dish.price | currency}}</span></h4>
<div class="col-xs-9 col-xs-offset-1">
<p>Put the comments here</p>


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

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

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

var dish = {
image: 'images/uthapizza.png',
category: 'mains',
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: [
comment:"Imagine all the eatables, living in conFusion!",
author:"John Lemon",
comment:"Sends anyone to heaven, I wish I could get my mother-in-law to eat it!",
author:"Paul McVites",
comment:"Eat it, just eat it!",
author:"Michael Jaikishan",
comment:"Ultimate, Reaching for the stars!",
author:"Ringo Starry",
comment:"It's your birthday, we're gonna party!",
author:"25 Cent",

}; = dish;





Could someone help me? I think it's something easy but know I have collapsed!

Thank you

Answer Source

You have at least 2 options.

Option 1: Use $scope. This will make sure your html remains unchanged

app.controller('dishDetailController', function($scope) {
   // Your dish declaration
   $ = dish;

Option 2: Javascript remains unchanged. In HTML, prefix dish with dishCtrl since you use dishDetailController as dishCtrl

Ex: Instead of <img class="media-object img-thumbnail" ng-src={{dish.image}} alt="Uthapizza">, use

<img class="media-object img-thumbnail" ng-src={{}} alt="Uthapizza">
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download