Chengyu Sun California State University, Los Angeles

Slides:



Advertisements
Similar presentations
JavaScript FaaDoOEngineers.com FaaDoOEngineers.com.
Advertisements

Cascading Style Sheets
CSS The basics { }. CSS Cascading Style Sheets - language used to – describe html appearance & formatting Style Sheet - file that describes – how html.
Chapter 7 © 2001 by Addison Wesley Longman, Inc. 1 Chapter 7 Sebesta: Programming the World Wide Web.
1 Using jQuery JavaScript & jQuery the missing manual (Second Edition)
Forms, Validation Week 7 INFM 603. Announcements Try placing today’s example in htdocs (XAMPP). This will allow you to execute examples that rely on PHP.
SUNY Polytechnic Institute CS 490 – Web Design, AJAX, jQueryAngularJS AngularJS is a client-side JavaScript Framework for adding interactivity to HTML.
JQuery 10/21. Today jQuery Some cool tools around the web JavaScript Libraries Drawing libraries HTML Frameworks Conventions.
JQUERY | INTRODUCTION. jQuery  Open source JavaScript library  Simplifies the interactions between  HTML document, or the Document Object Model (DOM),
Client Scripting1 Internet Systems Design. Client Scripting2 n “A scripting language is a programming language that is used to manipulate, customize,
INTRODUCTION TO HTML5 Using jQuery with HTML5. Introducing jQuery  Although it is not a part of any W3C or WHATWG specification, jQuery performs an important.
JavaScript - A Web Script Language Fred Durao
Controllers and Markup Controllers, $scope, Markup, Directives, Expressions, Binding, Filters, Validation SoftUni Team Technical Trainers Software University.
INTRODUCTION TO HTML5 New HTML5 User Interface and Attributes.
Lesson 16. Practical Application 1 We can take advantage of JavaScript and the DOM, to set up a form so that the first text box of a form automatically.
Angular 2.
IN THIS LESSON, WE WILL BECOME FAMILIAR WITH HTML AND BEGIN CREATING A WEB PAGE IN YOUR COMPUTER HTML – the foundation.
JavaScript and Ajax (JavaScript Environment) Week 6 Web site:
CSE 154 LECTURE 9: THE DOM TREE. The DOM tree The elements of a page are nested into a tree-like structure of objects the DOM has properties and methods.
Getting Started with Angular 2 and TypeScript Nebraska.Code() 2016 Spencer Schneidenbach Ryvit.
1 Using jQuery JavaScript & jQuery the missing manual (Second Edition)
HTML LAYOUTS. CONTENTS Layouts Example Layout Using Element Example Using Table Example Output Summary Exercise.
ANGULAR 2. JavaScript is a high-level, dynamic, untyped, and interpreted programming language. JavaScript was originally developed in May 1995 by Brendan.
Angularjs 2.0 : Getting started
REEM ALMOTIRI Information Technology Department Majmaah University.
CS320 Web and Internet Programming Handling HTTP Requests Chengyu Sun California State University, Los Angeles.
Introduction to.
The building blocks of Angular
Build in Objects In JavaScript, almost "everything" is an object.
In this session, you will learn to:
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
Bare boned notes.
WWU Hackathon May 6 & 7.
>> Introduction to CSS
CS5220 Advanced Topics in Web Programming JavaScript and jQuery
© 2015, Mike Murach & Associates, Inc.
Unit M Programming Web Pages with
>> CSS Rules Selection
WWU Hackathon May 6 & 7.
© 2016, Mike Murach & Associates, Inc.
Web DevelopmEnt Angular 4
Directives & Forms Capturing User Input SoftUni Team
JavaScript Forms Adding User Input.
Website Design 3
Displaying Form Validation Info
Web Systems Development (CSC-215)
DHTML Javascript Internet Technology.
Web Programming A different world! Three main languages/tools No Java
DHTML Javascript Internet Technology.
In Class Programming BIS1523 – Lecture 11.
CS5220 Advanced Topics in Web Programming Angular – Routing
15 minute break.
CS320 Web and Internet Programming MVC Architecture
CS3220 Web and Internet Programming Handling HTTP Requests
CS5220 Advanced Topics in Web Programming Angular – TypeScript
CS5220 Advanced Topics in Web Programming Angular – Routing
Chengyu Sun California State University, Los Angeles
CS5220 Advanced Topics in Web Programming Angular – Services
Angular 2 : CRUD Operations
Chengyu Sun California State University, Los Angeles
Chengyu Sun California State University, Los Angeles
CS5220 Advanced Topics in Web Programming Angular – TypeScript
Web Programming and Design
Web Programming and Design
CS5220 Advanced Topics in Web Programming Angular – Services
Web Programming and Design
Web Programming and Design
© 2017, Mike Murach & Associates, Inc.
Modern Front-end Development with Angular JS 2.0
Presentation transcript:

Chengyu Sun California State University, Los Angeles CS5220 Advanced Topics in Web Programming Angular – Components and Directives Chengyu Sun California State University, Los Angeles

index.html The "single page" in SPA The page that hosts all the components such as <app-root>

Component User-defined tags as opposed to standard HTML tags like <button> Appearance: HTML and CSS Behavior: TypeScript Web Components are supported at the browser level

@Component selector: name of the tag templateUrl or template styleUrls or styles More options

NgModule: The Angular Module System … The one-file-per-module approach of CommonJS/ES6 is limiting file file file file file file file file

… NgModule: The Angular Module System A NgModule group multiple files (i.e. components, services, and so on) into one module file file file file file file file file NgModule NgModule

@NgModule … Each module must export a class that is decorated with @NgModule declarations: components/directives/pipes that belong to this module imports: modules which this module depends on

… @NgModule exports: components/directives/pipes that can be used by other modules providers: injectable objects (usually services) provided by this module bootstrap: the component(s) to load when this module is used to bootstrap (i.e. to start) an application

Bootstrap An Application maint.ts : bootstrapModule(AppModule) app.module.ts : bootstrap: [AppComponent] app.component.html : Other components

About NgModule declarations and exports deal with components/directives/pipes, i.e. things used in templates While components/directives/pipes can be "private", services are always "public" imports deals with modules imports vs import

Example: Guest Book (Single Page) My Guest Book Add Comment John says: Hello! 4/29/2018 Jane says: Your website looks nice. 4/11/2018 Joe says: Nice to meet you. 3/5/2018

Create A New Component guest-book ng generate component <name> Generated files Naming conventions for components Tags and files Classes and methods

GuestBook – Data Data model class ngOnInit() is one of the component lifecycle hooks called after the component is constructed Code suggestion and auto import in Visual Studio Code

GuestBook – Display Properties of a component class can be accessed directly in the component template Interpolation: {{}} Directive: ngFor Pipe: date

Directive Encapsulated DOM-related logic that can be used to compose user interface Types of directives Component (i.e. directive with a view) Structural directive, e.g. ngIf, ngFor, ngSwitch Attribute directive, e.g. ngStyle and ngClass

ngFor Variable can have any name <div *ngFor="let entry of entries"> {{entry.name}} </div> Property <div *ngFor="let entry of entries; let index = index"> {{index}}. {{entry.name}} </div> Variable Variable can have any name Properties: index, first, last, even, odd

ngIf <p *ngIf="entries.length == 0"> No entries yet. </p> <p *ngIf="entries.length == 0; else hasEntries"> No entries yet. </p> <ng-template #hasEntries> <p>There are some entries.</p> </ng-template> Name of a element

ngSwitch <div [ngSwitch]="day"> <span *ngSwitchCase="1">Monday</span> <span *ngSwitchCase="2">Tuesday</span> <span *ngSwitchCase="3">Wednesday</span> <span *ngSwitchCase="4">Thursday</span> <span *ngSwitchCase="5">Friday</span> <span *ngSwitchDefault>Weekend</span> </div>

ngStyle An object <p [ngStyle]="{color: 'red', background: 'yellow'}"> Some text </p> <p [style.color]="'blue'">hello</p> A string

ngClass CSS classes: .red { color: red; } .yellow { background: yellow; } <p [ngClass]="'red'">Some text</p> <p [ngClass]="['yellow', 'red']">Some text</p> <p [ngClass]="{yellow: true, red: false}">Some text</p>

Pipes Chain-able formatting operators that can be applied to expressions {{entry.date | date:'M/d/yyy'}} {{3.1415 | number:'1.2-2' | currency:'USD'}}

The Component Hierarchy AppComponent

Example App Component GuestBook Component AddEntry Component

Pass Data from Parent to Child guest-book.component.ts: @Input() entries: GuestBookEntry[]; app.component.html: <app-guest-book [entries]="entries"> An input property in the child component A property in the current component

AddEntryComponent Template A variable in the view representing the current element Name: <input type="text" #name> <br> Message: <input type="text" #message> <br> <button (click)="addEntry(name, message)">Add</button> Event Event handler in the component

AddEntryComponent Code Angular is still JavaScript DOM events like click DOM elements like HTMLInputElement Event handler return false to stop event propagation

Pass Data from Child to Parent … add-entry.component.ts: @Output() entryAdded: EventEmitter<GuestBookEntry> = new EventEmitter<GuestBookEntry>(); this.entryAdded.emit( new GuestBookEntry(name.value, message.value));

… Pass Data from Child to Parent app.component.html: <app-add-entry (entryAdded)="addEntry($event)"> app.component.ts: addEntry(entry: GuestBookEntry): void { this.entries.push(entry); }

To Component Or Not To Component Putting everything inside one component seems to make coding easier (i.e. no need to pass data between components), so when is it better to create more components??