Professional Documents
Culture Documents
menu item is
being displayed on the web page. You can see the image,
the name of the menu item, a label, and the badge there for
displaying the price of the item. And then the description
of the menu item there. Going back to the code, you'll now
see that I am making use of Angular expressions in order to initialize many
of those fields in the media object. In particular,
let's take a look at the h2 class here. For this h2 class,
I am including the dish.name. Now this is the property
from the JavaScript object that we have initialized
in the ng-init earlier. This I am enclosing inside double braces
to indicate that this is an expression, an Angular expression. And so,
this will be substituted by the value of dish.name, by when Angular
initializes this page. Similarly, I am using dish.label
inside an Angular expression here, and for the badge, I am using dish.price. Not
e that I am sending this dish.price
through a currency filter here. Now, we're going to look at
filters in a later lesson. But in this example,
you'll see that the price, when it is piped through
the currency filter, will turn this stream
into a currency value. With appropriate dollar sign
to be displayed on the screen. The description itself
is included using this dish.description expression
inside a p tag here. Now, this is what ends up creating
the menu item on our web page. Taking a look at the dish object
inside the ng-init directive, you can notice that it has another field
called comment, which is currently empty. Now I'm gonna harness the power of
Angular in order to initialize or in order to change the value
of the comment here. Let me add in a few more HTML
code into the media object. And then we'll see how we can
make use of it to automatically update the comment field
of the dish object. I have added in a little
bit of HTML code here. Let's take a look at the web page,
and see the behavior first. Then we'll come back to understand
what this code is doing at this point. When you go to the HTML page, you will
notice that I have this comment with an empty area being displayed here,
and then I have an input box here. Which will allow me to
type in the comment. Watch what happens as I type in
the comment into the input box. What you are noticing is that as I type in
the information into the input box there, the comment in the web pages
automatically getting updated. This is the two-way data binding
of Angular coming into play. Taking a look at the code that we added
just recently, you saw that in the input tag here,
I am using the ng-model directive here. Now, the ng-model directive
essentially allows me to do a two-way data binding with the data. Now this, I am
binding this to the dish.comment field
in the dish object here. And also notice that the comment P tag that I have intr
oduced there contains
the dish.comment as an expression there. So as I type in the value
into the input field here, because Angular provides two-way data
binding, the information is immediately updated in the comment expression here a
nd
displayed on the web page. So this is the two-way data binding
of Angular in action for you. In order to demonstrate to you how
I can have multiple objects and automatically create a list of menu items
on the web page, I'm going to go in and change the dish object into an array
of JavaScript objects here. So I'm gonna replace the ng-init with an array of Ja
vaScript objects here. Note that now I have changed the ng-init
into an array of JavaScript objects here. Now, I have essentially
introduced an array of four different dishes here, and