[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

AngularJS directive issue

Posted on 2016-09-15
6
Medium Priority
?
105 Views
Last Modified: 2016-09-17
The code below does not display my images. It works for a similar file but this its giving me fits. I believe I am not using the directive.  properly. please let me know if I need to provide more info. In my editor I see the image for file so the image location is recognized by html page.

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

<head>
     <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="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>

<body>

    <div class="container">
        
    <div class="row row-content" ng-controller="dishDetailController as dishCtrl">
            <li class="media" ng-model="dishc in dishCtrl.dish" > <!--| filter:menuCtrl.filtText">-->

                    
                <div class="media-left media-middle">
                        <a href="#">
                        <img class="media-object img-thumbnail"
                         ng-src={{dishc.image}} alt="Uthappizza">
                        </a>
                    </div>
                    <div class="media-body">
                        <h2 class="media-heading">{{dishc.name}}
                         <span class="label label-danger">{{dishc.label}}</span>
                         <span class="badge">{{dishc.price | currency}}</span></h2>
                         <p>{{dishc.description}}</p>

                    </div>
                </li>    

                </div>
            </div>
            <div class="col-xs-9 col-xs-offset-1">
                <p>Put the comments here</p>
            </div>
        

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

        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:"Sends anyone to heaven, I wish I could get my mother-in-law to eat it!",
                                   author:"Paul McVites",
                                   date:"2014-09-05T17:57:28.556094Z"
                               },
                               {
                                   rating:3,
                                   comment:"Eat it, just eat it!",
                                   author:"Michael Jaikishan",
                                   date:"2015-02-13T17:57:28.556094Z"
                               },
                               {
                                   rating:4,
                                   comment:"Ultimate, Reaching for the stars!",
                                   author:"Ringo Starry",
                                   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;
            
        });

    </script>

</body>

</html>

Open in new window

Screen-Shot-2016-09-16-at-1.02.43-AM.png
0
Comment
Question by:Leo Torres
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 59

Expert Comment

by:Julian Hansen
ID: 41801024
I believe line 31 is the problem
<li class="media" ng-model="dishc in dishCtrl.dish" >

Open in new window

You are using an ng-model instead of ng-repeat. Should be
<li class="media" ng-repeat="dishc in dishCtrl.dish" >

Open in new window

0
 
LVL 8

Author Comment

by:Leo Torres
ID: 41801339
I tried ng-repeat and the result was worst. Here is my result
result
Also i only have 1 dish item as you can see below in the code.
here is what the final product should look like.
final
0
 
LVL 59

Accepted Solution

by:
Julian Hansen earned 2000 total points
ID: 41801429
Ok - that is helpful - you will need to help us here we don't know what you are aiming at.

What was your intention here?
<li class="media" ng-model="dishc in dishCtrl.dish" >

Open in new window

You cannot use that expression for ng-model?

There are a number of things that you need to fix with the code

1. The ng-model - you don't need this on the <li> I see what you were trying to do but that is not the correct way - see below for an explanation

2. Your comments are outside of the controller block - which means you won't be able to access the comments - you need to put that inside the controller block.

Let's break your code down.
You have elected to use the controller as approach instead of scope - which is good but here is how it works.

In your controller you have the structure called dish. You are making this a controller property by this line
(A)
this.dish = dish;

Open in new window


In your html you have
<div class="row row-content" ng-controller="dishDetailController as dishCtrl">

Open in new window

That means to access your model you need to use the controller alias in this case dishCtrl

Going back to the code snippet (A) your controller representation of your data is dish - therefore to output the values of your model you need to reference them as

dishCtrl.dish.<property>

Open in new window


So for the image this would be
dishCtrl.dish.image

Open in new window

In your comments section you would use an ng-repeat - I updated your code
<body>
<div class="container">
  <div class="row row-content" ng-controller="dishDetailController as dishCtrl">
    <li class="media"> <!--| filter:menuCtrl.filtText">-->
      <div class="media-left media-middle">
        <a href="#">
          <img class="media-object img-thumbnail"
          ng-src={{dishCtrl.dish.image}} alt="Uthappizza">
        </a>
      </div>
      <div class="media-body">
        <h2 class="media-heading">{{dishCtrl.dish.name}}
        <span class="label label-danger">{{dishCtrl.dish.label}}</span>
        <span class="badge">{{dishCtrl.dish.price | currency}}</span></h2>
        <p>{{dishCtrl.dish.description}}</p>

      </div>
    </li>    

    <div class="col-xs-9 col-xs-offset-1">
      <ul>
        <li ng-repeat="comment in dishCtrl.dish.comments">
          <h3>{{comment.rating}} stars</h3>
          <div>{{comment.comment}}</div>
          <div>&dash; {{comment.author}}, {{comment.date | date}}</div>
        </li>
      </ul>
    </div>
  </div>
</div>

<script src="../bower_components/angular/angular.min.js"></script>
<script>
var app = angular.module('confusionApp',[]);
app.controller('dishDetailController', function() {

  var dish={
    name:'Uthapizza',
    image: 'images/foreground1.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:"Sends anyone to heaven, I wish I could get my mother-in-law to eat it!",
        author:"Paul McVites",
        date:"2014-09-05T17:57:28.556094Z"
      },
      {
        rating:3,
        comment:"Eat it, just eat it!",
        author:"Michael Jaikishan",
        date:"2015-02-13T17:57:28.556094Z"
      },
      {
        rating:4,
        comment:"Ultimate, Reaching for the stars!",
        author:"Ringo Starry",
        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;

});
</script>
</body>

Open in new window

you can see it working here
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Author Comment

by:Leo Torres
ID: 41802593
Thank you for being an excellent resource. I was using ng-model just because I thought it was the right directive.
0
 
LVL 8

Author Closing Comment

by:Leo Torres
ID: 41802594
Thanks again!
0
 
LVL 59

Expert Comment

by:Julian Hansen
ID: 41802678
ng-model is for binding a view element to your model - i.e. elements that have a value.

To access the view for output or expressions you either use the controllerAs prefix to dereference the property you want in your controller - or if you are not using controller as (scope) then you can refer to the property directly.

Glad to be of help and you are most welcome.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Find out what you should include to make the best professional email signature for your organization.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question