Angular template-syntax elements

<h2>Hero List</h2>

<p><i>Pick a hero from the list</i></p>
<ul>
  <li *ngFor="let hero of heroes" (click)="selectHero(hero)">
    {{hero.name}}
  </li>
</ul>

<app-hero-detail *ngIf="selectedHero" [hero]="selectedHero"></app-hero-detail>

  • represents a new component, HeroDetailComponent
  • defines the hero-detail child view of HeroListComponent

Data Binding

{{hero.name}}, (click), [hero]

  • bind program data to and from the DOM

1. directives

  • Directives give instructions to Angular on how to render the templates to the DOM
  • A directive can be defined in Angular as a class with the @Directive decorator
  • A component is a special kind of directive with a template assoiciated to it

directive types: Structural and Attribute

2. Structural directive

  • alter the layout by adding, removing and replacing elements in DOM
  • Apply a structural directive to a host element in the DOM and its descendents
    (e.g. ngFor structural directive)

Common Structural Directives

  • NgIf
<div *ngIf="selectedDish">...</div>

[value = true, the div is added into the dump]
if the selectedDish is not null then this div will be added to the DOM.
[value = false, removing the div]
If that selectedDish is null, then this div, whatever is contained in this div, will not be added to the DOM

NgFor

<mat-list-item *ngFor="let dish of dishes">

this allows us to iterate over the array of dishes that are defined in our type script code

NgSwitch
This allows you to selectively add certain elements to the DOM by specifying a condition, depending on what the condition evaluates to, then you will be using one of those elements among the list.

Practise

Objective:

  • Make use of the Angular material grid list component to display a list of items.
  • Use the material Card component to display detailed information.
  • Use a built-in Angular pipe to turn a word into uppercase in the template.

Updating the Menu Template

  • Open menu.component.html and update its content as follows:
<div class="container"
     fxLayout="column"
     fxLayoutGap="10px">

  <div fxFlex>
    <div>
      <h3>Menu</h3>
      <hr>
    </div>
  </div>

  <div fxFlex>
    <mat-grid-list cols="2" rowHeight="200px">
      <mat-grid-tile *ngFor="let dish of dishes">
        <img height="200px" src={{dish.image}} alt={{dish.name}}>
        <mat-grid-tile-footer>
          <h1>{{dish.name | uppercase}}</h1>
        </mat-grid-tile-footer>
      </mat-grid-tile>
    </mat-grid-list>
  </div>

</div>
  • Here we are using the Grid list Angular material component to display the information.
  • Open app.module.ts and update it as follows:
. . .

import { MatGridListModule } from '@angular/material/grid-list';
import { MatCardModule } from '@angular/material/card';
import { MatButtonModule } from '@angular/material/button';

. . .

  imports: [
    . . .,
    MatGridListModule,
    MatCardModule,
    MatButtonModule,
    . . .
  ],

. . .
  • Also, update the menu.component.ts file as follows to move the details of the dishes into a constant, in preparation for introducing services in a future exercise:
 . . .
 
 const DISHES: Dish[] = [
 . . .
 
 ];
 
 . . .
 
 export class MenuComponent implements OnInit {

  dishes = DISHES;

  selectedDish = DISHES[0];

 . . .
 
 }

Add a Card ComponentUpdate

  • the menu.component.html template to display the details of a selected dish using the Material Card component as follows:
  <div fxFlex *ngIf="selectedDish">
    <mat-card>
      <mat-card-header>
        <mat-card-title>
          <h3>{{selectedDish.name | uppercase}}</h3>
        </mat-card-title>
      </mat-card-header>
      <img mat-card-image src={{selectedDish.image}} alt={{selectedDish.name}}>
      <mat-card-content>
        <p>{{selectedDish.description}}
        </p>
      </mat-card-content>
      <mat-card-actions>
        <button mat-button>LIKE</button>
        <button mat-button>SHARE</button>
      </mat-card-actions>
    </mat-card>
  </div>

Check the size of pages on different screen

  • open ‘developer tools’ on chrome
  • click the button ‘toggle devise toolbar’
    在这里插入图片描述
  • observe the pages on different screen
    在这里插入图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值