官术网_书友最值得收藏!

Template data binding 

Now that we have a service that connects to an API endpoint and receives a JSON file, let's make some small changes to our views, known as templates in the Angular world. The templates are the HTML files inside of the module folder:

  1. Open src/app/app.component.html and remove all of the code before the <router-outlet></route-outlet> tags.
  2. Open src/app/beers/beers.component.html and add the following code, right after the beers works paragraph:
<div class="row">
<div class="col" href="" *ngFor="let item of beersList">
<figure>
<img [src]="item.image_url" [alt]="item.name" />
<figcaption>
<h1>{{item.name}}</h1>
<p>{{item.tagline}}</p>
</figcaption>
</figure>
</div>
</div>

Note that we are using the curly brace template tags ({{}}) and a *ngFor directive to display our data. Let's look at some Angular data binding types:

{{ some.property }} One way Binding
[(ngModel)]="some.value" Two way Binding
(click)="showFunction($event)" Event Binding
  1. Now, we need to add some style to beers.component.html; open src/app/beers/beers.component.css and add the following code:
body {
margin: 40px;
}
.row {
display: grid;
grid-template-columns: 300px 300px 300px;
grid-gap: 10px;
background-color: #fff;
color: #444;
}
.col {
background-color: #d1d1d1;
border-radius: 5px;
padding: 10px;
}
figure {
text-align: center;
}
img {
height:250px;
}

We are now very close to completing our example application. The last step is to build our application and see the final result.

主站蜘蛛池模板: 驻马店市| 望江县| 波密县| 莆田市| 什邡市| 顺义区| 普洱| 二连浩特市| 浦北县| 新和县| 军事| 吉林省| 绥芬河市| 巩义市| 电白县| 祁阳县| 永定县| 新泰市| 安丘市| 昌图县| 辽宁省| 仙居县| 蒙阴县| 肥城市| 岢岚县| 什邡市| 枞阳县| 建平县| 红原县| 奉化市| 同心县| 太和县| 万宁市| 盐山县| 芦溪县| 梧州市| 虹口区| 会同县| 庆阳市| 讷河市| 金溪县|