ANGULAR2 OVERVIEW. The Big Picture. Getting Started. Modules and Components. Declarative Template Syntax. Forms

Similar documents
Chapter 1 - Model Driven Forms

Advantages: simple, quick to get started, perfect for simple forms, don t need to know how form model objects work

Angular 2 Programming

Lab 1 - Introduction to Angular

"Charting the Course... Comprehensive Angular 5. Course Summary

ANGULAR 2.X,4.X + TYPESRCIPT by Sindhu

"Charting the Course... Comprehensive Angular. Course Summary

Angular 4 Training Course Content

Learn to Build Awesome Apps with Angular 2

IN4MATX 133: User Interface Software

By the end of this Angular 6 tutorial, you'll learn by building a real world example application:

Angular 2: What s new? Jonas Bandi, IvoryCode GmbH

Angular 2 and TypeScript Web Application Development

"Charting the Course... Comprehensive Angular 6 Course Summary

Course Outline. ProTech Professional Technical Services, Inc. Comprehensive Angular 7 Course Summary. Description

PHP WITH ANGULAR CURRICULUM. What you will Be Able to Achieve During This Course

Front End. Presentation Layer. UI (User Interface) User <==> Data access layer

Each class (which we will talk about in the next section) you see in an Ionic application will

One Framework. Angular

THE PRAGMATIC INTRO TO REACT. Clayton Anderson thebhwgroup.com WEB AND MOBILE APP DEVELOPMENT AUSTIN, TX

Chapter 1 - Development Setup of Angular

Integrating Angular with ASP.NET Core RESTful Services. Dan Wahlin

Learn to Build Awesome Apps with Angular 2

Stencil: The Time for Vanilla Web Components has Arrived

Angular 2 and TypeScript Web Application Development

Extending VMware vcloud Director User Interface Using Portal Extensibility Ticketing Example

Advance Mobile& Web Application development using Angular and Native Script

Angular 4 Syllabus. Module 1: Introduction. Module 2: AngularJS to Angular 4. Module 3: Introduction to Typescript

FRONT END WEB. {< Course Details >}

AngularJS Introduction

AngularJS Fundamentals

Comprehensive AngularJS Programming (5 Days)

PHP + ANGULAR4 CURRICULUM 6 WEEKS

React. HTML code is made up of tags. In the example below, <head> is an opening tag and </head> is the matching closing tag.

ANGULAR 10. Christian Ulbrich (Zalari)

Understanding this structure is pretty straightforward, but nonetheless crucial to working with HTML, CSS, and JavaScript.

AngularJS Intro Homework

INTRODUCTION TO IONIC 2

Cascading Style Sheet

Understanding Angular Directives By Jeffry Houser

ANGULARJS INTERVIEW QUESTIONS

1 CS480W Quiz 6 Solution

Techniques for Optimizing Reusable Content in LibGuides

Static Webpage Development

Single Page Applications using AngularJS

An Brief Introduction to a web browser's Document Object Model (DOM) Lecture 20 COMP110 Spring 2018

Nagaraju Bende

JavaScript Rd2. -Kyle Simpson, You Don t Know JS

Sample Copy. Not For Distribution.

Lab 7 Macros, Modules, Data Access Pages and Internet Summary Macros: How to Create and Run Modules vs. Macros 1. Jumping to Internet

Views for Developers. What is Views? (and data geeks) "a tool for making lists of stuff" Bec White DrupalCamp WI, July 2010

Practical Course: Web Development Angular JS Part I Winter Semester 2016/17. Juliane Franze

Financial. AngularJS. AngularJS.

The magic behind. Angular 2+

Lesson4:CreatinganInterfaceandChecklistService

Financial. AngularJS. AngularJS. Download Full Version :

Angular2. Bernhard Niedermayer. Software Development playing around with Angular2 since alpha.

Introduction to. Angular. Prof. Dr.-Ing. Thomas Wiedemann.

The core of Tapestry's form support is the Form component. The Form component encloses (wraps around) all the other field components such

DNCMagazine. ANGULAR. Cheat Sheet

Lesson 1 using Dreamweaver CS3. To get started on your web page select the link below and copy (Save Picture As) the images to your image folder.

Jquery Manually Set Checkbox Checked Or Not

Before proceeding with this tutorial, you should have a basic understanding of HTML, CSS, JavaScript, TypeScript, and Document Object Model (DOM).

Front-End Web Developer Nanodegree Syllabus

Frontend Frameworks. SWE 432, Fall 2016 Design and Implementation of Software for the Web

Chapter 1 - Consuming REST Web Services in Angular

Comprehensive Angular 2 Review of Day 3

Lesson: Web Programming(6) Omid Jafarinezhad Sharif University of Technology

Using Dreamweaver CC. 6 Styles in Websites. Exercise 1 Linked Styles vs Embedded Styles

International Research Journal of Engineering and Technology (IRJET) e-issn: Volume: 05 Issue: 06 June p-issn:

Web Application Development

Introduction to WEB PROGRAMMING

Setting up an Angular 4 MEAN Stack (Tutorial)

Web development using PHP & MySQL with HTML5, CSS, JavaScript

Web Site Development with HTML/JavaScrip

Lecture 8. ReactJS 1 / 24

AngularJS AN INTRODUCTION. Introduction to the AngularJS framework

Demystifying Angular 2. SPAs for the Web of Tomorrow

Unveiling the Basics of CSS and how it relates to the DataFlex Web Framework

Learn Web Development CodersTrust Polska course outline. Hello CodersTrust! Unit 1. HTML Structuring the Web Prerequisites Learning pathway.

JAVASCRIPT - CREATING A TOC

Deccansoft Software Services

Client Side JavaScript and AJAX

welcome to BOILERCAMP HOW TO WEB DEV

With great power comes great responsibility

Decoupled Drupal with Angular

Advanced React JS + Redux Development

Angular 2. Useful links. Allen Holub 2-1

Modern and Responsive Mobile-enabled Web Applications

Layered UI and Adaptation Policies for Ubiquitous Web Applications

CS193X: Web Programming Fundamentals

Sections and Articles

Siteforce Pilot: Best Practices

UNIT -II. Language-History and Versions Introduction JavaScript in Perspective-

CS 1100: Web Development: Client Side Coding / Fall 2016 Lab 2: More HTML and CSS

Templates and Databinding. SWE 432, Fall 2017 Design and Implementation of Software for the Web

PHP & PHP++ Curriculum

UI Course HTML: (Html, CSS, JavaScript, JQuery, Bootstrap, AngularJS) Introduction. The World Wide Web (WWW) and history of HTML

<Insert Picture Here> JavaFX 2.0

PIC 40A. Midterm 1 Review

Transcription:

FORMS IN ANGULAR Hello Cluj. I m Alex Lakatos, a Mozilla volunteer which helps other people volunteer. I want to talk to you today about Angular forms. What s a form you ask? A form creates a cohesive, effective, and compelling data entry experience. An Angular form coordinates a set of data-bound user controls, tracks changes, validates input, and presents errors.

ANGULAR2 OVERVIEW The Big Picture Getting Started Modules and Components Declarative Template Syntax Forms Angular 2 is all about building blocks. So if you re someone who likes to build things this is a fun story to hear about!

Now, these aren t just any type of building block. These legos can be re-used in various ways and have the power to build applications that can target multiple devices and browsers.

When the building blocks are combined you can build some cool things! Being recruited by the dark side? Build the next enterprise-scale app for the death star (since I m sure they ll build another one).

Being recruited by the rebel alliance to build an app? You can do that too of course.

Calendar Component Menu Component Grid Component Header Component

BUILDING SPAS WITH COMPONENTS Header Component Menu Compone nt Grid List Component Footer Component

BUILDING SPAS & COMPONENTS WITH ANGULAR Angular 1 Angular 2 Actively Develop ed (1.5.x) Continue d Support Released v4 TypeScri pt, ES6 or ES5

ANGULAR 2 CORE CONCEPTS Components Modules Decorators Languages (TypeScript, ES6, ES5) Data Binding Services Dependency Injection Consistency!

TypeScript ES7 ES6 ES 5

Angular 2 is all about building blocks. So if you re someone who likes to build things this is a fun story to hear about! GETTING STARTED

ANGULAR 2 BUILDING BLOCKS Modules Compone nts Decorator s Services

STEPS TO GET STARTED <html> <body> 3 1 <script> System.import('app'); </script> </body> </html> index.html Bootstrap <app-component></app-component> @Component() class AppComponent { } 2

Bootstrap App Component Routes Modules Loader Template Decorator Component Component Code Directives Service

Angular 2 is all about building blocks. So if you re someone who likes to build things this is a fun story to hear about! MODULES AND COMPONENTS

WHAT IS A COMPONENT? A component is a reusable object Made up of: Code HTML Template Has a "selector": <customers></customers>

WHAT S IN A COMPONENT CLASS? imports import { Component } from '@angular/core'; import { DataService } from '../services/data.service'; decorators @Component({ selector: 'customers', templateurl: 'customers.component.html' }) class export class CustomersComponent { }

@angular/http custom/3rd party @angular/core @angular/router @angular/common

EXPORTING CUSTOM MODULES export class DataService {... }

IMPORTING MODULES customers.component.ts Imported module is relative to current file. import { Component } from '@angular/core'; import { DataService } from '../shared/services/data.service';...

SYSTEM.JS: ENABLING MODULE LOADING <script src="node_modules/zone.js/dist/zone.js"></script> <script src="node_modules/reflect-metadata/reflect.js"></script> <script src="node_modules/systemjs/dist/system.js"></script> <script src="systemjs.config.js"></script> <script> System.import('app').catch(function(err){ console.error(err); }); </script> Import this module as a starting point

DECLARATIVE TEMPLATE SYNTAX

COMPONENTS AND TEMPLATES @Component({ <customers></customers> selector: 'customers', providers: [DataService], inject the DataService templateurl: 'customers.component.html', directives: [FilterTextboxComponent, SortByDirective] }) export class CustomersComponent { Uses these directives constructor(private dataservice: DataService) { } } Angular 2 apps are built using components Provides reuse and consistency Components rely on decorators to define metadata Can use directives for rendering in component templates

ONE-WAY BINDING SYNTAX Bind to DOM property <button <div [disabled]="!isenabled">save</button> [hidden]="!isvisible" [class.active]="isactive">...</div> Binding and change detection relies on a tree of components (no cycles) Create one-way bindings using [property] or bind-property syntax

HANDLING EVENTS Handle click event <button (click)="save()">save</button> <th sort-by="firstname" (sorted)="sort($event)">...</th> banana in a box Angular 2 simplifies working with events and eliminates the need for many directives Handle an event using the (event) or on-event syntax

TWO-WAY BINDING SYNTAX <input type="text" [(ngmodel)]="model.filter" /> Define a binding that detects changes and updates the property value Create two-way bindings using [(property)] or bindon-property syntax Change triggers an event that Angular uses to update the property value

BUILT-IN DIRECTIVES Angular 2 directive that generates a template <tr *ngfor="let customer of filteredcustomers"> <td>{{ customer.firstname }}</td> <td>{{ customer.lastname }}</td> </tr> <div *ngif="customer">{{ customer.details }}</ div>

FORMS IN ANGULAR Template Forms import { FormsModule } from @angular/forms'; Reactive Forms import { ReactiveFormsModule } from '@angular/forms' Angular offers two form-building technologies: reactive forms and template-driven forms. The two technologies belong to the @angular/forms library and share a common set of form control classes. But they diverge markedly in philosophy, programming style, and technique. They even have their own modules: the ReactiveFormsModule and the FormsModule.

TEMPLATE FORMS Component Class HTML-based Template An Angular form has two parts: an HTML-based template and a component class to handle data and user interactions programmatically. We begin with the class because it states, in brief, what our form can do.

COMPONENT CLASS There s nothing special about this component, nothing form-specific, nothing to distinguish it from any component you've written before.

HTML-BASED TEMPLATE Notice that we also added a name attribute to our <input> tag and set it to "name" which makes sense for the hero's name. Any unique value will do, but using a descriptive name is helpful. Defining a name attribute is a requirement when using [(ngmodel)] in combination with a form.

HTML-BASED TEMPLATE Our hero must choose one super power from a fixed list of Agency-approved powers. We maintain that list internally (in HeroFormComponent). We'll add a select to our form and bind the options to the powers list using ngfor.

CHANGE TRACKING & VALIDATION A form isn't just about data binding. We'd also like to know the state of the controls in our form. Using ngmodel in a form gives us more than just a two way data binding. It also tells us if the user touched the control, if the value changed, or if the value became invalid. The NgModel directive doesn't just track state; it updates the control with special Angular CSS classes that reflect the state. We can leverage those class names to change the appearance of the control.

We can use CSS to display a visual indicator of the validity state of the form. CHANGE TRACKING & VALIDATION

ERROR HANDLING We can do better. The Name input box is required and clearing it turns the bar red. That says something is wrong but we don't know what is wrong or what to do about it. We can leverage the control's state to reveal a helpful message. We need a template reference variable to access the input box's Angular control from within the template. Here we created a variable called name and gave it the value "ngmodel".

REACTIVE FORMS AbstractControl FormControl FormGroup FormArray AbstractControl is the abstract base class for the three concrete form control classes: FormControl, FormGroup, and FormArray. It provides their common behaviors and properties, some of which are observable. FormControl tracks the value and validity status of an individual form control. It corresponds to an HTML form control such as an input box or selector. FormGroup tracks the value and validity state of a group of AbstractControl instances. The group's properties include its child controls. The top-level form in your component is a FormGroup. FormArray tracks the value and validity state of a numerically indexed array of AbstractControl instances.

FormControls Here you are creating a FormControl called name. It will be bound in the template to an HTML input box for the hero name. A FormControl constructor accepts three, optional arguments: the initial data value, an array of validators, and an array of async validators. This simple control doesn't have data or validators. In real apps, most form controls have both.

FormControls Usually, if you have multiple FormControls, you'll want to register them within a parent FormGroup. This is simple to do. Notice that now the single input is in a form element. The novalidate attribute in the <form> element prevents the browser from attempting native HTML validations. formgroup is a reactive form directive that takes an existing FormGroup instance and associates it with an HTML element. In this case, it will associate the FormGroup you saved as heroform with the form element.

FormBuilder The FormBuilder class helps reduce repetition and clutter by handling details of control creation for you. FormBuilder.group is a factory method that creates a FormGroup. FormBuilder.group takes an object whose keys and values are FormControl names and their definitions. In this example, the name control is defined by its initial data value, an empty string. Defining a group of controls in a single object makes for a compact, readable style. It beats writing an equivalent series of new FormControl(...) statements.

AVAILABLE VALIDATIONS The FormBuilder declaration object specifies the three controls of the sample's hero form. Each control spec is a control name with an array value. The first array element is the current value of the corresponding hero field. The (optional) second value is a validator function or an array of validator functions. Most of the validator functions are stock validators provided by Angular as static methods of the Validators class. Angular has stock validators that correspond to the standard HTML validation attributes.

CUSTOM VALIDATIONS The function is actually a factory that takes a regular expression to detect a specific forbidden name and returns a validator function. In this sample, the forbidden name is "bob"; the validator rejects any hero name containing "bob". Elsewhere it could reject "alice" or any name that the configuring regular expression matches. The forbiddennamevalidator factory returns the configured validator function. That function takes an Angular control object and returns either null if the control value is valid or a validation error object. The validation error object typically has a property whose name is the validation key ('forbiddenname') and whose value is an arbitrary dictionary of values that we could insert into an error message ({name}).

CHANGE TRACKING FormControl.valueChanges Observer ngonchanges

WHAT S THE DIFFERENCE async vs sync directive vs code unit testing In reactive forms, you create the entire form control tree in code. You can immediately update a value or drill down through the descendants of the parent form because all controls are always available. Template-driven forms delegate creation of their form controls to directives. To avoid "changed after checked" errors, these directives take more than one cycle to build the entire control tree. That means you must wait a tick before manipulating any of the controls from within the component class. The asynchrony of template-driven forms also complicates unit testing. You must wrap your test block in async() or fakeasync() to avoid looking for values in the form that aren't there yet. With reactive forms, everything is available when you expect it to be. Which is better, reactive or template-driven? Neither is "better". They're two different architectural paradigms, with their own strengths and weaknesses. Choose the approach that works best for you. You may decide to use both in the same application.

MULTUMESC! @Lakatos88 Alex Lakatos