AngularJS Introduction

Similar documents
AngularJS AN INTRODUCTION. Introduction to the AngularJS framework

What is AngularJS. à Javascript Framework à MVC à for Rich Web Application Development à by Google

AngularJS. Beginner's guide - part 1

AngularJS. Beginner's guide - part 2

Building Web Applications

ANGULARJS INTERVIEW QUESTIONS

AngularJS Fundamentals

ANGULARJS - MOCK TEST ANGULARJS MOCK TEST II

Creating Effective Websites using AngularJS

Financial. AngularJS. AngularJS. Download Full Version :

Financial. AngularJS. AngularJS.

Nagaraju Bende

3 Days Training Program

Understanding Angular Directives By Jeffry Houser

CS142 Winter 2017 Midterm Grading Solutions and Rubric

Comprehensive AngularJS Programming (5 Days)

Web Application Development

Front End Programming

Design and Implementation of Single Page Application Based on AngularJS

IN4MATX 133: User Interface Software

Single Page Applications

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

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

Single Page Applications using AngularJS

Future Web App Technologies

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

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

Getting Started with

Questions and Answers from Lukas Ruebbelke s AngularJS In-Depth Course

<angular/> JavaScript Done Right

a Very Short Introduction to AngularJS

HTML DOM IN ANGULARJS

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

React Not Just Hype!

Angular 2 Programming

Lecture 8. ReactJS 1 / 24

Sample CS 142 Midterm Examination

Full Stack Web Developer

P a g e 1. Danish Technological Institute. Scripting and Web Languages Online Course k Scripting and Web Languages

THE HITCHHIKERS GUIDE TO. Harper Maddox CTO, EdgeTheory 30 September 2014

Tim Roes. Android- & inovex in Karlsruhe. GDG Karlsruhe Co-Organizer.

Sample CS 142 Midterm Examination

STARCOUNTER. Technical Overview

Index LICENSED PRODUCT NOT FOR RESALE

HyperText Markup Language (HTML)

Source. Developer Guide / forms

CodeValue. C ollege. Prerequisites: Basic knowledge of web development and especially JavaScript.

Comprehensive Angular 2 Review of Day 3

React.js. a crash course. Jake Zimmerman January 29th, 2016

Varargs Training & Software Development Centre Private Limited, Module: HTML5, CSS3 & JavaScript

Sample Copy. Not For Distribution.

The course also includes an overview of some of the most popular frameworks that you will most likely encounter in your real work environments.

Static Webpage Development

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

This course is designed for web developers that want to learn HTML5, CSS3, JavaScript and jquery.

Introduction to using HTML to design webpages

ADVANCED JAVASCRIPT #8

Introduction to AngularJS

IN ACTION. Lukas Ruebbelke. FOREWORD BY Martin Gontovnikas MANNING SAMPLE CHAPTER

High Performance Single Page Application with Vue.js

AngularJS Step By Step Tutorials. $interval([function], [delaytime], [count], [invokeapply], [parameter]);

Angular 4 Training Course Content

JavaScript Specialist v2.0 Exam 1D0-735

Full Stack Web Developer

COMS W3101: SCRIPTING LANGUAGES: JAVASCRIPT (FALL 2018)

Input and Validation. Mendel Rosenblum. CS142 Lecture Notes - Input

Advance Mobile& Web Application development using Angular and Native Script

Modern and Responsive Mobile-enabled Web Applications

React(.js) the Domino Way High-Performance Client for Domino. Knut Herrmann

Sample CS 142 Midterm Examination

Qiufeng Zhu Advanced User Interface Spring 2017

TechWatch Report Javascript Libraries and Frameworks

Stencil: The Time for Vanilla Web Components has Arrived

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

Table of Contents. Introduction 1. 1 Jumping Into JavaScript 5. for Loops 17

Make a Website. A complex guide to building a website through continuing the fundamentals of HTML & CSS. Created by Michael Parekh 1

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

Building mobile app using Cordova and AngularJS, common practices. Goran Kopevski

Web Development and HTML. Shan-Hung Wu CS, NTHU

Frontend UI Training. Whats App :

A conditional statement can compare two values. Here we check if one variable we declared is greater than another. It is true so the code executes.

MPT Web Design. Week 1: Introduction to HTML and Web Design

Etanova Enterprise Solutions

Vue.js Framework. Internet Engineering. Spring Pooya Parsa Professor: Bahador Bakhshi CE & IT Department, Amirkabir University of Technology

Developing ASP.NET MVC Web Applications (486)

Personal Information Manager Overview & Installation Guide

Dynamic Web Development

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

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

HTML + CSS. ScottyLabs WDW. Overview HTML Tags CSS Properties Resources

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

Founder & CEO

PHP & PHP++ Curriculum

JavaScript Framework: AngularJS

Web Architecture and Development

Getting MEAN. with Mongo, Express, Angular, and Node SIMON HOLMES MANNING SHELTER ISLAND

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

Web Programming and Design. MPT Senior Cycle Tutor: Tamara Week 1

Ten interesting features of Google s Angular Project

AngularJS Intro Homework

Transcription:

AngularJS Introduction Mendel Rosenblum

AngularJS JavaScript framework for writing web applications Handles: DOM manipulation, input validation, server communication, URL management, etc. Considered opinionated Uses Model-View-Controller pattern HTML Templating approach with two-way binding Minimal server-side support dictated Focus on supporting for programming in the large and single page applications Modules, reusable components, testing, etc. Widely used framework (Angular 1-2009) with a major rewrite available (Angular 2) CS142 will use Angular 1

Angular Concepts and Terminology Template Directive Scope Compiler Data Binding Dependency Injection Module Service HTML with additional markup used to describe what should be displayed Allows developer to extend HTML with own elements and attributes (reusable pieces) Context where the model data is stored so that templates and controllers can access Processes the template to generate HTML for the browser Syncing of the data between the Scope and the HTML (two ways) Fetching and setting up all the functionality needed by a component A container for all the parts of an application A way of packaging functionality to make it available to any view

Angular Example <!doctype html> <html ng-app> <head> <script src="./angular.min.js"></script> </head> <body> <div> <label>name:</label> <input type="text" ng-model="yourname" placeholder="enter a name here"> <h1>hello {{yourname}}!</h1> </div> </body> </html>

Angular Bootstrap <!doctype html> <html ng-app> <head> <script src="./angular.min.js"></script> </head> <body> <div> <label>name:</label> Script loads and runs when browser signals context is loaded and ready <input type="text" ng-model="yourname" placeholder="enter a name here"> <h1>hello {{yourname}}!</h1> </div> </body> </html>

Angular Bootstrap <!doctype html> <html ng-app> <head> Once ready, scans the html looking for a ng-app attribute - Creates a scope. <script src="./angular.min.js"></script> </head> <body> <div> <label>name:</label> <input type="text" ng-model="yourname" placeholder="enter a name here"> <h1>hello {{yourname}}!</h1> </div> </body> </html>

Angular Bootstrap <!doctype html> <html ng-app> <head> <script src="./angular.min.js"></script> </head> <body> <div> <label>name:</label> Compiler - Scans DOM covered by the ng-app looking for templating markup - Fills in with information from scope. <input type="text" ng-model="yourname" placeholder="enter a name here"> <h1>hello {{yourname}}!</h1> </div> </body> </html>

Angular Compiler Output Changes to template HTML in red. Classes: ng-scope - Angular attached a scope here. ng-binding - Angular bound something here. <!doctype html> ng-pristine/ng-dirty - User interactions? <html ng-app class="ng-scope"> ng-untouched/ng-touched - Blur event? <head> ng-valid/ng-invalid - Valid value? <script src="./angular.min.js"></script> </head> <body> <div> <label>name:</label> <input type="text" ng-model="yourname" placeholder="enter a name here" class="ng-pristine ng-untouched ng-valid"> <h1 class="ng-binding">hello!</h1> </div> </body> </html> Note: {{yourname}} replaced with value of yourname

Two-way binding: Type 'D' character into input box <!doctype html> <html ng-app class="ng-scope"> <head> <script src="./angular.min.js"></script> </head> <body> <div> <label>name:</label> <input type="text" ng-model="yourname" placeholder="enter a name here" class="ng-valid ng-dirty ng-valid-parse ng-touched"> <h1 class="ng-binding">hello D!</h1> </div> </body> </html> The scope variable yourname is updated to be "D" and the template is rerendered with yourname = "D". Note angular validation support

Two-way binding: Type 'a', 'n' into input box <!doctype html> <html ng-app class="ng-scope"> <head> <script src="./angular.min.js"></script> </head> <body> <div> <label>name:</label> <input type="text" ng-model="yourname" placeholder="enter a name here" class="ng-valid ng-dirty ng-valid-parse ng-touched"> <h1 class="ng-binding">hello Dan!</h1> Template updated with each change </div> (i.e. keystroke)! </body> </html>

angular.module In a JavaScript file: <!doctype html> <html ng-app="cs142app"> <head> <script src="./angular.min.js"></script> </head> <body> <div> <label>name:</label> angular.module("cs142app", []); or to fetch existing module: angular.module("cs142app"); <input type="text" ng-model="yourname" placeholder="enter a name here"> <h1>hello {{yourname}}!</h1> </div> </body> </html> Module - Container of everything needed under ng-app

Controllers angular.module("cs142app", []).controller('mycntrl', function($scope) { <!doctype html> $scope.yourname = ""; $scope.greeting = "Hola"; <html ng-app="cs142app"> }); <head> <script src="./angular.min.js"></script> </head> <body ng-controller="mycntrl"> <div> <label>name:</label> In a JavaScript file: Will define a new scope and call controller MyCntrl. <input type="text" ng-model="yourname" placeholder="enter a name here"> <h1>{{greeting}} {{yourname}}!</h1> </div> </body> </html>

Templates, Scopes & Controllers Best practice: Each template component gets a new scope and is paired with a controller. Expressions in templates: {{foo + 2 * func()}} are evaluated in the context of the scope. Controller sets up scope: $scope.foo =... ; $scope.func = function() {... }; Best practice: Keep expressions simple put complexity in controller Controllers make model data available to view template

Scope inheritance A scope object gets its prototype set to its enclosing parent scope <div ng-controller="crtl1"> <div ng-controller="crtl2">... </div> </div> ScopeB's prototype points at ScopeA Mostly does what you want (all properties of A appear in B) Useful since scopes are frequently created (e.g. ng-repeat, etc.) $rootscope is parent of all Creates new scope (ScopeA) Creates new scope (ScopeB)

"There should always be a dot in your model" Common advice to beginning AngularJS programmers. Why? Consider: <input type="text" ng-model="yourname" placeholder="enter a name here"> Model reads will go up to fetch properties from inherited scopes. Writes will create the property in the current scope! <input type="text" ng-model="model.yourname" placeholder="enter a name here"> Read of object model will locate it in whatever inherited scope it is in. yourname will be create in that object in the right scope.

Scope digest and watches Two-way binding works by watching when expressions in view template change and updating the corresponding part of the DOM. Angular add a watch for every variable or function in template expressions During the digest processing all watched expressions are compared to their previously known value and if different the template is reprocessed and the DOM update Angular automatically runs digest after controller run, etc. It is possible to: Add your own watches: ($scope.$watch(..)) (e.g. caching in controller) Trigger a digest cycle: ($scope.$digest()) (e.g. model updates in event)

Example of needing scope $watch Name: {{firstname}} {{lastname}} vs Name: {{fullname}} $scope.fullname = $scope.firstname + " " + $scope.lastname; $scope.$watch('firstname', function() { $scope.fullname = $scope.firstname + " " + $scope.lastname; });

A digression: camelcase vs dash-case Word separator in multiword variable name Use dash: active-buffer-entry Capitalize first letter of each word: activebufferentry Issue: HTML is case-insensitive so camelcase is a problem AngularJS solution: You can use either, Angular will map them to the same thing. Use dash in HTML and camelcase in JavaScript Example: ng-model and ngmodel

ngrepeat - Directive for looping in templates ngrepeat - Looping for DOM element creation (tr, li, p, etc.) <ul> <li ng-repeat="person in peoplearray"> <span>{{person.name}} nickname {{person.nickname}}</span> </li> </ul> Powerful but opaque syntax. From documentation: <div ng-repeat="model in collection orderby: 'id' as filtered_result track by model.id">

ngif/ngshow - Conditional inclusion in DOM ngif - Include in DOM if expression true (dialogs, modals, etc.) <div class="center-box" ng-if="showtrialoverwarning"> {{buyproductadmonishmenttext}} </div> Note: will create scope/controllers when going to true, exit going to false ngshow - Like ngif except uses visibility to hide/show DOM elements Occupies space in DOM structure (but not on screen) when hidden Scope & controllers created at startup

ngclick/ngmodel - Binding user input to scope ngclick - Run code in scope when element is clicked <button ng-click="count = count + 1" ng-init="count=0"> Increment </button> <span> count: {{count}} </span> ngmodel - Bind with input, select, textarea tags <select name="singleselect" ng-model="data.singleselect"> <option value="option-1">option 1</option> <option value="option-2">option 2</option> </select>

nghref & ngsrc Sometimes need to use ng version of attributes: a tag <a ng-href="{{linkhref}}">link1</a> img tag <img ng-src="{{imagesrc}}" alt="description" />

nginclude - Fetches/compile external HTML fragment Include partial HTML template (Take angular expression of URL) <div ng-include="'navbarheader.html'"</div> CS142 uses for components <div ng-include="'components/example/exampletemplate.html'" ng-controller="examplecontroller"></div>

Directives Angular preferred method for building reusable components Package together HTML template and Controller and extend templating language Ng prefixed items in templates are directives Directive can: Be inserted by HTML compiler as: attribute (<div my-dir="foo">...</div>) element (<my-dir arg1="foo">...</my-dir>) Specify the template and controller to use Accept arguments from the template Run as a child scope or isolated scope Powerful but with a complex interface Example: <example arg1="foobar"></example>

Directives are heavily used in Angular <body layout="row" ng-controller="appctrl"> <md-sidenav layout="column"... > <md-toolbar...>... </md-toolbar> <md-list> <md-list-item ng-repeat="item in menu"> <md-item-content layout="row" layout-align="start center"> <md-button aria-label="add" ng-click="showadd($event)"> </md-item-content> </md-list-item> </md-list> <md-divider></md-divider> <md-subheader>management</md-subheader>

Services Used to provide code modules across view components Example: shared JavaScript libraries Angular has many built-in services Server communication (model fetching) $http, $resource, $xhrfactory Wrapping DOM access (used for testing mocks) $location, $window, $document, $timeout, $interval Useful JavaScript functionality $animate, $sce, $log Angular internal accesses $rootscope, $parse, $compile

Dependency injection Support for programming in large a. Entities list what they define and what they need b. At runtime Angular brings entities and their dependencies together Example: var cs142app = angular.module('cs142app', ['ngroute']); cs142app.config(['$routeprovider', function($routeprovider) { cs142app.controller('maincontroller', ['$scope',function($scope) {

Angular APIs ngroute - Client-side URL routing and URL management CS142 - Passing parameters to the views ngresource - REST API access CS142 - Fetch models ngcookies - Cookie management and access ngaria - Support for people with disabilities (Accessible Rich Internet Applications) ngtouch - Support for mobile devices (ngswipeleft, ngswiperight, etc.) nganimate - Support for animations (CSS & JavaScript based) ngsanitize - Parse and manipulate HTML safely

Some thoughts on JavaScript Frameworks Web app can not start until framework downloaded and initialized Particular relevant for wimpy devices and networks (e.g. Mobile) Can lazy load Angular modules (Makes dependency tracking important) Core Angular is not small 1.4.8/angular.js 1.4.8/angular.min.js 1.4.8/angular.min.js.gzip 1,070,726 bytes 148,199 bytes 53,281 bytes

Beyond AngularJS ReactJS - Facebook designed view component only JavaScript with HTML (JSX) embedded in it One way binding Render into a virtual DOM - Server-side rendering Angular (née AngularJS 2) is a significant change from AngularJS Kept HTML templates No longer works out of DOM - Better performance, enable server-side rendering Dropped scopes, controllers, directives, services,. JavaScript => TypeScript Classes defined "Component" having HTML template, CSS, and class methods Less opinionated