angular2-chapter03

Exploring the New Template Syntax

Check code at: https://github.com/wghglory/angular2-fundamental

*ngFor

events-list.Component has events array: [event] is child component object name

import { Component } from '@angular/core'

@Component({
    selector: 'events-list',
    template: `

Upcoming Angular 2 Events

` }) export class EventsListComponent { events = [ { id: 1, name: 'Angular Connect', date: '9/26/2036', time: '10:00 am', price: 599.99, imageUrl: '/app/assets/images/angularconnect-shield.png', location: { address: '1057 DT', city: 'London', country: 'England' } }, ... ] }

event-thumbnail.component:

Note: event? will not throw err if event is null

event?.location.address doesn't need location?, event? is good enough

import {Component, Input} from '@angular/core'

@Component({
    selector: 'event-thumbnail',
    template: `
        

Event: {{event?.name}}

Price: \${{event?.price}}
Date: {{event?.date}}
Time: {{event?.time}}
Address: {{event?.location.address}}, {{event?.location.city}}, {{event?.location.country}}
`, styles: [` .thumbnail{min-height:210px;} .margin-left{margin-left:10px;} .well div{color:#bbb;} `] }) export class EventThumbnailComponent { @Input() event: any }

*ngIf -- will comment out the dom, performance factor

if we know each event is not null, but some of them has location property, some don't but have onlineUrl property, in this case, we should use event.location?.address

if events array has 2 element, first has location but not onlineUrl, second has onlineUrl but not location.

import {Component, Input} from '@angular/core'

@Component({
    selector: 'event-thumbnail',
    template: `
        

Event: {{event?.name}}

Price: \${{event?.price}}
Date: {{event?.date}}
Time: {{event?.time}}
Address: {{event?.location?.address}}, {{event?.location?.city}}, {{event?.location?.country}}
Online Url: {{event?.onlineUrl}}
`, styles: [` .thumbnail{min-height:210px;} .margin-left{margin-left:10px;} .well div{color:#bbb;} `] }) export class EventThumbnailComponent { @Input() event: any }

[hidden]

the above *ngIf code can be replaced by [hidden], but ngif performance is better than hidden in this case:

Address: {{event?.location?.address}}, {{event?.location?.city}}, {{event?.location?.country}}
Online Url: {{event?.onlineUrl}}

when you don't want to render a heavy DOM element, use *ngIf, so element comments out
when the element frequently shows/hides, use [hidden], so element style display none

[ngSwtich]

@Component({
    selector: 'event-thumbnail',
    template: `
            
Time: {{event?.time}} (Early Start) (Late Start) (Normal Start)
` })

[ngClass]

  1. will add yellow class if event?.time === '8:00 am'
@Component({
    selector: 'event-thumbnail',
    template: `
            
Time: {{event?.time}}
`, styles: [` .yellow{color:yellow !important;} `] })
  1. what if we need to add 2 classes? use ngClass
@Component({
    selector: 'event-thumbnail',
    template: `
            
Time: {{event?.time}}
`, styles: [` .yellow{color:yellow !important;} .bold{font-weight:800;} `] })
  1. use function after ngClass
  • ngClass won't affect native class
  • ngClass can take string, array, object
@Component({
    template: `
            
Time: {{event?.time}}
`, styles: [` .yellow{color:yellow !important;} .bold{font-weight:800;} .notaffected{font-size:18px;} `] }) export class EventThumbnailComponent { getClasses() { // const isEarly = this.event && this.event.time === '8:00 am'; // return { yellow: isEarly, bold: isEarly }; // if (this.event && this.event.time === '8:00 am') // return 'yellow bold'; // return ''; if (this.event && this.event.time === '8:00 am') return ['yellow', 'bold'] return [] } }

[ngStyle]

  1. only 1 style: use [style.property]=""
  1. multiple styles by ngStyle
import {Component, Input} from '@angular/core'

@Component({
    selector: 'event-thumbnail',
    template: `
            
            
Time: {{event?.time}}
` }) export class EventThumbnailComponent { getStyles(): any { if (this.event && this.event.time === '8:00 am') return { 'text-decoration': 'underline', 'font-style': 'italic' } return {} } }

你可能感兴趣的:(angular2-chapter03)