Angular

Junghoo Cho

cho@cs.ucla.edu

Angular Overview

What We Will Learn

Angular CLI (1)

Angular CLI (2)

Angular Component

Generating Components

Developing HTML Template

Directive

Component Directive

Data Binding

Interpolation

Property Binding

Event Binding

Attribute Directives in Component

Attribute Directives in Component

HTML Element vs Angular Component

Component as User-Defined HTML Element

More on Property Binding (1)

More on Property Binding (2)

More on Event Binding

Throwing Custom Event

Throwing Custom Event

Angular $event Object

<!-- app.component.html -->
<app-search-box (advice)="handleAdvice($event);"></app-search-box>   

Structural Directive

Structural Directive: *ngIf

<img [src]="imgUrl" *ngIf="imgUrl">

Structural Directive: *ngFor

<ul>
    <li *ngFor="let item of items">{{ item.name }}</li>
</ul>

Structural Directive: ngSwitch

<ng-container [ngSwitch]="media.type">
    <img [src]="media.url" *ngSwitchCase="'image'">
    <video [src]="media.url" *ngSwitchCase="'video'"></video>
    <embed [src]="media.url" *ngSwitchDefault>
</ng-container>

Summary So Far

Back to Google Suggest

Functions of Two Components

Component “API”

DisplayComponent

<!-- display.component.html -->
<ul>
    <li *ngFor="let suggestion of suggestions">{{suggestion}}</li>   
</ul>

// display.component.ts
import { Input } from '@angular/core';

@Input() suggestions: string[];

SearchBoxComponent

<!-- search-box.component.html -->
<form action="http://www.google.com/search">
    <input type="text" name="q" (input)="handleInput($event);">
    <input type="submit" (click)="handleSubmit($event)">
</form>

// search-box.component.ts
import { EventEmitter, Output } from '@angular/core';

query: string = "";
@Output() userInput = new EventEmitter<string>();
@Output() submit = new EventEmitter<Event>();

SearchBoxComponent

// search-box.component.ts
handleInput(event) {
    this.query = event.target.value;
    if (this.noUSC(this.query)) {
        this.userInput.emit(this.query);
    } else {
        alert("No USC query please!");
    }
}
handleSubmit(event) { 
    if (this.noUSC(this.query)) {
        this.submit.emit(event);
    } else {
        alert("No USC query please!");
        event.preventDefault();
    }
}
noUSC(query) { return !(/(^| )USC($| )/i.test(query)); }

Next Step

Handling userInput Event

<!-- app.component.html -->
<app-search-box (userInput)="getSuggestions($event)"></app-search-box>
<app-display [suggestions]="suggestions"></app-display>
// app.component.ts
suggestions: string[] = [];
getSuggestions(query: string) {
    // for now, we just show user input as suggestions
    this.suggestions = [query];
}

Summary: Angular Component

Angular Service

Creating Service

SuggestionService: Implementation

// suggestion.service.ts
async getSuggestions(query: string): Promise<string[]> {
    let res = await fetch("http://oak.cs.ucla.edu/classes/cs144/examples/google-suggest.php?q="+encodeURI(query));
    let text = await res.text();
    let parser = new DOMParser();
    let xml = parser.parseFromString(text,"text/xml");
    let s = xml.getElementsByTagName('suggestion');
    let suggestions = [];
    for (let i = 0; i < s.length; i++) {
        suggestions.push(s[i].getAttribute("data"));
    }
    return suggestions; // automatically converted to Promise with async
}

Dependency Injection (1)

Dependency Injection (2)

Dependency Injection: Example

// app.component.ts
import { SuggestionService } from './suggestion.service';     
...
public suggestionService: SuggestionService;

constructor(suggestionService: SuggestionService) {
    this.suggestionService = suggestionService;
}
// app.component.ts
import { SuggestionService } from './suggestion.service';
...   
constructor(public suggestionService: SuggestionService) {}   

Using Service

// app.component.ts
import { SuggestionService } from './suggestion.service';

constructor(public suggestionService: SuggestionService) {}   

async getSuggestions(query) {
    if (query.length > 0) {
        this.suggestions = await this.suggestionService.getSuggestions(query);
    }
}

What We Learned

Other Topics for Self Study

References