AngularJS - Polito.it

3y ago
52 Views
3 Downloads
810.53 KB
35 Pages
Last View : 1d ago
Last Download : 3m ago
Upload by : Bria Koontz
Transcription

AngularJSBeginner's guide - part 1

AngularJS:20/04/2017AngularJS: beginner's Guide - part 12

AngularJS: Superheroic JavaScript MVWFramework20/04/2017AngularJS: beginner's Guide - part 13

AngularJS: Superheroic JavaScript MVWFramework20/04/2017AngularJS: beginner's Guide - part 14

AngularJS: Superheroic JavaScript MVWFramework Javascript framework for writing frontend webapps20/04/2017AngularJS: beginner's Guide - part 15

AngularJS: Superheroic JavaScript MVWFramework Javascript framework for writing frontend webapps It aims at simplifying both the development andthe testing of web application20/04/2017AngularJS: beginner's Guide - part 16

Example 1 !DOCTYPE html html lang "en" ng-app head meta charset "UTF-8" title What's your name? /title script src "./angular.min.js" /script /head script loads and runs when the body browser signals that the contextis ready. div label Name /label input type "text" ng-model "name" placeholder "Enteryour name" h1 Hello span ng-bind "name" /span ! /h1 /div /body /html 20/04/2017AngularJS: beginner's Guide - part 17

Example 1 !DOCTYPE html Angular scans the HTML looking html lang "en" ng-app for the ng-app attribute. It head creates a scope. meta charset "UTF-8" title What's your name? /title script src "./angular.min.js" /script /head body div label Name /label input type "text" ng-model "name" placeholder "Enteryour name" h1 Hello span ng-bind "name" /span ! /h1 /div /body /html 20/04/2017AngularJS: beginner's Guide - part 18

Example 1 !DOCTYPE html html lang "en" ng-app head meta charset "UTF-8" title What's your name? /title script src "./angular.min.js" /script /head The compiler scans the DOM for body templating markups. Then, it fills themwith info from the scope. div label Name /label input type "text" ng-model "name" placeholder "Enteryour name" h1 Hello span ng-bind "name" /span ! /h1 /div /body /html 20/04/2017AngularJS: beginner's Guide - part 19

Example 1 !DOCTYPE html html lang "en" ng-app head meta charset "UTF-8" title What's your name? /title script src "./angular.min.js" /script /head body Two-way databinding byng-bind div label Name /label input type "text" ng-model "name" placeholder "Enteryour name" h1 Hello span ng-bind "name" /span ! /h1 /div name is replaced with the valueinserted in the input /body /html 20/04/2017AngularJS: beginner's Guide - part 110

AngularJS Initialization1. AngularJS initializesautomatically uponDOMContentLoaded event2. AngularJS looks in thetemplate for the ngAppdirective which designates ourapplication root.3. Loads the module associatedwith the directive.4. Creates the application 20/04/20175. Compiles the DOM treating thengApp directive as the root ofthe compilationAngularJS: beginner's Guide - part 111

AngularJS Initialization1. AngularJS initializesautomatically uponDOMContentLoaded event2. AngularJS looks in thetemplate for the ngAppdirective which designates ourapplication root.3. Loads the module associatedwith the directive.4. Creates the application 5. Compiles the DOM treating thengApp directive as the root ofthe compilationTip: The ng-app attribute represents an AngularJS directive, named ngApp (AngularJS uses kebab-case for its customattributes and camelCase for the corresponding directives which implement them)20/04/2017AngularJS: beginner's Guide - part 112

Concepts and TerminologyTemplateHTML with additional markup used to describe what shouldbe displayedDirectiveAllows a developer to extend HTML with her own elementsand attributesModuleA container for parts of an applicationScopeContext where the model data is stored so that templatesand controllers can access itCompilerProcesses the template to generate HTML for the browserDependencyInjectionFetching and setting up all the functionality needed by acomponentData BindingSyncing of data between the Scope and the HTML (two-way)ServiceReusable functionality available for any view20/04/2017AngularJS: beginner's Guide - part 113

Directives They are markers on a DOM element that tellAngular’s HTML compiler to attach a specificbehavior to that element– The ng-app directive defines an AngularJS application html lang "en" ng-app – The ng-model directive binds the value of HTML controls (input,select, textarea) to application data. input type "text" ng-model "name" – The ng-bind directive binds application data to the HTML view. span ng-bind "name" /span 20/04/2017AngularJS: beginner's Guide - part 114

Two-Way Data Binding It is the automatic synchronization of databetween the model and the view components20/04/2017AngularJS: beginner's Guide - part 115

Two-Way Data Binding It is the automatic synchronization of databetween the model and the view componentsHow can we do it? AngularJS binds data to HTML using Expressions:- ng-bind directive h1 Hello span ng-bind " expression " /span ! \h1 - Double braces h1 Hello {{ expression }}! \h1 AngularJS will resolve the expression, and return theresult exactly where the expression is written20/04/2017AngularJS: beginner's Guide - part 116

Example 2: double braces !DOCTYPE html html lang "en" ng-app head meta charset "UTF-8" title What's your name? /title script src "./angular.min.js" /script /head body Two-way databinding withdouble braces div label Name /label input type "text" ng-model "name" placeholder "Enteryour name" h1 Hello {{ name }}! Math says 5 4 is {{ 5 4 }} /h1 name is replaced with the value inserted in the /div input and 5 4 is replace with 9 /body /html 20/04/2017AngularJS: beginner's Guide - part 117

Other Built-in Directives ng-init– Evaluates the given expression(s) to, for example, create avariable when initiating the application ng-src ng-href– Add an image or a link, where the src is evaluated byAngularJS ng-repeat– instantiate a template once per item from a collection div ng-init "names ['Laura', 'Teo', 'Gabriella']" h3 Loop through names with ng-repeat /h3 ul li ng-repeat "name in names" {{ name }} /li /ul /div 20/04/2017AngularJS: beginner's Guide - part 1Example 318

Other Built-in Directives ng-show/ng-hide– show/hide DOM elements according to a givenexpression ng-if– include an element in the DOM if the subsequentexpression is true ng-click– Angular version of HTML’s onclick– execute a given operation when the element isclicked20/04/2017AngularJS: beginner's Guide - part 119

Filters Formats the value of an expression for displayto the user– {{ name uppercase }} Keeps formatting into the presentation Syntax– {{ expression filter }}– {{ expression filter1 filter2 }}– {{ expression filter:argument }}20/04/2017AngularJS: beginner's Guide - part 120

AngularJS: MVW or MV*20/04/2017AngularJS: beginner's Guide - part 121

AngularJS: MVW or MV* View– It is the visible result generated by AngularJS byapplying some directives to the HTML templateExample:20/04/2017AngularJS: beginner's Guide - part 122

AngularJS: MVW or MV* Whatever: the AngularJS Controller– A JavaScript function– It defines a new scope that may contain data (properties) specify some behaviors (methods)– It should contain only the logic needed for a singleview i.e., each view should have one controller (and viceversa)20/04/2017AngularJS: beginner's Guide - part 123

AngularJS: MVW or MV* Whatever: the AngularJS Controller– It should be concerned – only! - with consuming data, preparing it for the view, and transmitting it to service for processing– Best practices services are responsible to hold the model and communicatewith a server declarations should manipulate the DOM views do not have to know about their controller a controller definitely does not want to know about its view20/04/2017AngularJS: beginner's Guide - part 124

!DOCTYPE html html lang "en" ng-app "sonetExample" head meta charset "UTF-8" title Introducing. Controllers! /title script src "./angular.min.js" /script /head body ng-controller "MyCtrl" div controller label Name /label input type "text" ng-model "name" . h1 {{ greeting }} {{name}}! /h1 /div [.]angular.module("sonetExample", []).controller('MyCtrl', function ( scope) { scope.name ""; scope.greeting "Hello";})HTMLmoduleJSExample 420/04/2017AngularJS: beginner's Guide - part 125

Modules Container to collect and organize components– in a modular way Multiple modules can exist in an application– “main” module (ng-app)– service modules, controller modules, etc. Best practices– a module for each feature– a module for each reusable component (especiallydirectives and filters)– an application level module which depends on the abovemodules and contains any initialization code20/04/2017AngularJS: beginner's Guide - part 126

The Scope ( scope) The “glue” between a controller and a template The scope is shared among the controller and theview (the template): the framework passes it as aparameter to the controllerangular.module("sonetExample", []).controller('MyCtrl', function ( scope) {.}) It aims at defining the data model and exposing itto the view20/04/2017AngularJS: beginner's Guide - part 127

The Scope ( scope) Every property and/or function defined within thescope will be available in the view[.] body ng-controller "MyCtrl" div label Name /label input type "text" ng-model "name" . h1 {{ greeting }} {{name}}! /h1 h1 {{ Bye() }}! /h1 /div [.]angular.module("sonetExample", []).controller('MyCtrl', function ( scope) { scope.name ""; scope.greeting "Hello"; scope.Bye function() {return "Bye" " " "!"};})20/04/2017AngularJS: beginner's Guide - part 1HTMLJSExample 528

The Scope ( scope) Thus, it can be seen as an execution context forexpressions like{{ pizza.name }} Scopes can watch expressions and propagateevents Scopes are arranged in a hierarchical structure thatmimic the DOM structure of the application:– every application has ONE root scope: rootScope– every controller has its own scope– all the scopes are descendant of the rootScope–20/04/2017continueAngularJS: beginner's Guide - part 129

The Scope ( scope)– if we define nested controllers, the most internal scopeinherits methods and properties of all the others[.] div ng-controller "userController" p Name: input type "text" ng-model "user.name" /p p Surname: input type "text" ng-model "user.surname" /p p ng-controller "greetingController" {{greeting()}} /p /div [.]HTMLangular.module("myApp", [])JS.controller("userController",function( scope) { scope.user {name: "Mario", surname: on( scope) { scope.greeting function() {return "Hello " scope.user.name " " scope.user.surname "!"};Example 6});20/04/2017AngularJS: beginner's Guide - part 130

The Scope ( scope) More info about the Scope– ENG https://docs.angularjs.org/guide/scope– ITA inding/ /04/2017AngularJS: beginner's Guide - part 131

20/04/2017AngularJS: beginner's Guide - part 132

References AngularJS official guide– https://docs.angularjs.org/guide AngularJS API documentation– https://docs.angularjs.org/api AngularJS in 60 minutes [video]– https://www.youtube.com/watch?v i9MHigUZKEM Learn Angular in your browser– http://angular.codeschool.com/20/04/2017AngularJS: beginner's Guide - part 133

01QYAPD SOCIAL NETWORKING: TECHNOLOGIESAND APPLICATIONSMy contact information:Teodoro Montanaro (teodoro.montanaro@polito.it)Thanks to Luigi De Russis (luigi.derussis@polito.it), thecreator the slides I used as basis!

License This work is licensed under the Creative Commons “AttributionNonCommercial-ShareAlike Unported (CC BY-NC-SA 3,0)” License. You are free:– to Share - to copy, distribute and transmit the work– to Remix - to adapt the work Under the following conditions:– Attribution - You must attribute the work in the manner specified by theauthor or licensor (but not in any way that suggests that they endorse youor your use of the work).– Noncommercial - You may not use this work for commercial purposes.– Share Alike - If you alter, transform, or build upon this work, you maydistribute the resulting work only under the same or similar license to thisone. To view a copy of this license, .0/20/04/2017AngularJS: beginner's Guide - part 135

2. AngularJS looks in the template for the ngApp directive which designates our application root. 3. Loads the module associated with the directive. 4. Creates the application injector 5. Compiles the DOM treating the ngApp directive as the root of the compilation AngularJS: beginner's Guide - part 1

Related Documents:

AngularJS uses dependency injection and make use of separation of concerns. AngularJS provides reusable components. AngularJS viii With AngularJS, the developers can achieve more functionality with short code. In AngularJS, views are pure html pages, and controllers written in JavaScript do the business processing. On the top of everything, AngularJS applications can run on all major browsers .

AngularJS Tutorial W3SCHOOLS.com AngularJS extends HTML with new attributes. AngularJS is perfect for Single Page Applications (SPAs). AngularJS is easy to learn. This Tutorial This tutorial is specially designed to help you learn AngularJS as quickly and efficiently as possible. First, you will learn the basics of AngularJS: directives, expressions, filters, modules, and controllers. Then you .

Beginning AngularJS Beginning AngularJS is your step-by-step guide to learning the powerful AngularJS JavaScript framework. AngularJS is one of the most respected and innovative frameworks for building properly structured, easy-to-develop web applications. This book will teach you the absolute essentials, from downloading and installing AngularJS, to using modules, controllers, expressions .

AngularJS provides data binding capability to HTML thus giving user a rich and responsive experience AngularJS code is unit testable. AngularJS uses dependency injection and make use of separation of concerns. AngularJS provides reusable components. With AngularJS,

INTRODUCTION TO THE ANGULARJS FRAMEWORK. AngularJS AngularJS is a very powerful JavaScript Framework for writing frontend web applications Used in Single Page Application (SPA) projects Inspired by the Model-View-Controller pattern Extends HTML DOM with additional attributes and

AngularJS Tutorial, AngularJS Example pdf, AngularJS, AngularJS Example, angular ajax example, angular filter example, angular controller Created Date 11/29/2015 3:37:05 AM

AngularJS team at Google as an external contractor and is a founder member of the AngularUI project. He has spoken about AngularJS at Devoxx UK and numerous London meetups. He also runs training courses in AngularJS. His consultancy practice is now primarily focused on helping businesses make best use of AngularJS. I would like to thank the team at Google for giving us AngularJS, in particular .

Code Explanation for ng-transclude Directive in AngularJS: 1. The ng-app specifies the root element ( myApp ) to define AngularJS . ng-transclude directive is used to include the existing content "AngularJS" . Sample Output for ng-transclude Directive in AngularJS: 1. The content welcome to wikitechy is displayed in the output using the .