SlideShare uma empresa Scribd logo
1 de 26
Associate Professor David Parsons
Massey University
Auckland, New Zealand
Outline
‱ AngularJS components
‱ Directives and Expressions
‱ Data binding
‱ Modules And Controllers
AngularJS
‱ A framework rather than a library
‱ Good choice for Single Page App development
‱ Extends HTML by adding new elements and
custom attributes that carry special meaning
Library
App Framework
App
AngularJS
‱ AngularJS is currently being
maintained by developers at Google
‱ Open source software released under
the MIT license
‱ Available for download at GitHub
‱ Called AngularJS because HTML uses
angle brackets
Model-View-Whatever
‱ AngularJS is an MVW framework
(Model-View-Whatever)
‱ Can be used to develop apps based on
either
– MVC (Model-View-Controller)
– MVVM (Model-View-ViewModel)
‱ aka naked objects
AngularJS Components
1. Model
The data shown to the users (JavaScript Objects)
2. View
This is what the users see (generated HTML)
3. Controller
The business logic behind an application
4. Scope
A context that holds data models and functions
5. Directives
Extend HTML with custom elements and attributes.
6. Expressions
Represented by {{}} in the HTML, can access models and functions
from the scope
7. Templates
HTML with additional markup in the form of directives and
expressions
Libraries
‱ The main library is the angular.js file
‱ Download from angularjs.org or use
the Google CDN
Directives
‱ Angular.js extends HTML with directives
‱ These directives are HTML attributes with an
‘ng’ prefix.
– Or ‘data-ng’ in HTML5
‱ Important directives:
– ng-app
‱ defines an AngularJS application
– ng-model
‱ binds the value of HTML controls to application data
– ng-bind
‱ binds application data to the HTML view
The ng-app Directive
‱ This is required for the page to use
Angular
‱ Applied to an HTML element
‱ The simplest version does not relate to
any external definition
– App name quotes are empty
<body ng-app="">
..
</body>
Expressions
‱ The main purpose of an expression is binding
data from the model to the view
‱ The expression is dynamically re-evaluated each
time any of the data it depends on changes
‱ Written inside double braces
‱ Result is included in the page where the
expression is written
‱ Simple examples:
– Arithmetic expressions
– String expressions
{{ 5 + 4 }}
{{ "Hello " + "World" }}
{{ expression }}
Testing Angular Configuration
‱ A simple expression is a good way of
checking that the angular library is
found and that you have the required
ng-app directive
Two-way Data Binding
‱ One important feature of Angular is the
way it binds values to expressions
‱ These values can be in the HTML
(view) or in JavaScript variables or
objects (model)
‱ Data binding is automatic
‱ Angular automatically updates bound
values
ng-model and ng-bind
‱ An HTML element that contains data
can be bound to a value in the model
‱ The innerHTML of another element can
be bound to that part of the model
<div ng-app="">
<p>Name: <input type="text" ng-model="name"></p>
<p ng-bind="name"></p>
</div>
The ng-init Directive
‱ This directive can be used to initialise
values in the model
‱ The ng-init directive can contain
multiple initialisations, separated by
semicolons
<div ng-app="" ng-init="name='Massey' ">
<p>Name: <input type="text" ng-model="name"></p>
<p ng-bind="name"></p>
</div>
ng-init="forename='Massey'; lastname='University' "
Binding an Expression
‱ The ng-bind directive can contain an
expression
‱ In this example it multiplies a data
value from the model (“number”) by
itself
<div ng-app="" ng-init="number=10">
<p>Number: <input type="text" ng-model="number" ></p>
<p>Square:</p>
<p ng-bind="number * number"> </p>
</div>
Modules
‱ Angular code in external files is
defined in modules
‱ The first parameter is the name of the
app module that can be referenced
from an ng-app directive
– The array is for any dependencies we may
have on other modules (can be empty)
var app = angular.module("ticketoffice", [ ]);
<div ng-app="ticketoffice">
Controllers
‱ Apps have controllers
‱ These are JavaScript functions
‱ Given a name when added to an app as a
controller
‱ Name your controllers using Pascal Case
– Controllers are really constructor functions
– These are usually named in JavaScript using
Pascal case
var app=angular.module("ticketoffice", []);
app.controller("TicketController", function() {
// body of function
});
The ng-controller Directive
‱ Angular uses the ng-controller
directive to call controller functions
‱ Here, TicketController shows an alert
(just as an example, to show it is being
called)
app.controller("TicketController", function(){
alert("TicketController called");
});
<div ng-controller="TicketController"> </div>
Object Data in the Controller
‱ The controller might access object
data
‱ In this case a travel ticket (‘traveldoc’)
app.controller("TicketController", function(){
this.traveldoc=ticket;
});
var ticket=
{
origin : "Wellington",
destination : "Auckland",
price : 110
}
Controller Alias
‱ To access data from the controller, we can use an
alias
‱ Inside the div, the alias can be used to access the
data from the controller, using expressions
– Note:
‱ This controller’s scope is within the div only
‱ Sometimes will need a broader scope
<div ng-controller="TicketController as agent">
<h2>Origin: {{agent.traveldoc.origin}}</h2>
<h2>Destination: {{agent.traveldoc.destination}}</h2>
<h3>Price: ${{agent.traveldoc.price}}</h3>
Multiple Objects
‱ We might have an array of objects
‱ We also need to change the controller,
since the name has changed, for
readability (from ‘ticket’ to ‘tickets’)
var tickets = [
{ origin : "Wellington", destination : "Auckland", price : 110},
{ origin : "Christchurch", destination : "Dundedin", price : 120},


];
this.traveldocs=tickets;
Array Access by Index
‱ Access by index is now possible, e.g.
‱ However, not good for displaying
multiple objects on the same page
<h2>{{agent.traveldocs[0].origin}}</h2>
<h2>{{agent.traveldocs[0].destination}}</h2>
<h3>${{agent.traveldocs[0].price}}</h3>
The ng-repeat Directive
‱ Can be used to iterate over an array of
objects
‱ Note the ‘in’
‱ The controller reference is moved to
the enclosing scope
<body ng-controller="TicketController as agent" >
<div ng-repeat="traveldoc in agent.traveldocs">
<h2>{{traveldoc.origin}}</h2>
<h2>{{traveldoc.destination}}</h2>
<h3>${{traveldoc.price}}</h3>
Adding Images with ng-src
‱ When adding images with Angular, we
need to use the ng-src directive,
instead of the standard ‘src’ attribute
of the ‘img’ tag
‱ Let’s assume our ticket objects each
include an array of images:
var tickets = [
{ origin : "Wellington", destination : "Auckland", price : 110, isAvailable : false,
images: [
{ full : "wellington1-full.jpg", thumb : "wellington1-thumb.jpg" },
{ full : "wellington2-full.jpg", thumb : "wellington2-thumb.jpg" },


Using ng-src
‱ In the HTML img tag, replace ‘src’ with
‘ng-src’, along with an Angular
expression to locate the image.
<img ng-src="{{traveldoc.images[0].full}}" />
Directive Summary
‱ Here is summary of the directives that we
have seen so far:
– ng-app
– ng-model
– ng-init
– ng-bind
– ng-controller
– ng-src
– ng-repeat

Mais conteĂșdo relacionado

Mais procurados

Angular - Chapter 5 - Directives
 Angular - Chapter 5 - Directives Angular - Chapter 5 - Directives
Angular - Chapter 5 - DirectivesWebStackAcademy
 
Angular 10 course_content
Angular 10 course_contentAngular 10 course_content
Angular 10 course_contentNAVEENSAGGAM1
 
Angular overview
Angular overviewAngular overview
Angular overviewThanvilahari
 
Angular - Chapter 4 - Data and Event Handling
 Angular - Chapter 4 - Data and Event Handling Angular - Chapter 4 - Data and Event Handling
Angular - Chapter 4 - Data and Event HandlingWebStackAcademy
 
Angular Data Binding
Angular Data BindingAngular Data Binding
Angular Data BindingDuy Khanh
 
Angular introduction students
Angular introduction studentsAngular introduction students
Angular introduction studentsChristian John Felix
 
REST API and CRUD
REST API and CRUDREST API and CRUD
REST API and CRUDPrem Sanil
 
Angular 2.0 forms
Angular 2.0 formsAngular 2.0 forms
Angular 2.0 formsEyal Vardi
 
Angular Data Binding
Angular Data BindingAngular Data Binding
Angular Data BindingJennifer Estrada
 
Angular 5 presentation for beginners
Angular 5 presentation for beginnersAngular 5 presentation for beginners
Angular 5 presentation for beginnersImran Qasim
 
Angular modules in depth
Angular modules in depthAngular modules in depth
Angular modules in depthChristoffer Noring
 
Angular Directives | Angular 2 Custom Directives | Angular Tutorial | Angular...
Angular Directives | Angular 2 Custom Directives | Angular Tutorial | Angular...Angular Directives | Angular 2 Custom Directives | Angular Tutorial | Angular...
Angular Directives | Angular 2 Custom Directives | Angular Tutorial | Angular...Edureka!
 
Angular Interview Questions & Answers
Angular Interview Questions & AnswersAngular Interview Questions & Answers
Angular Interview Questions & AnswersRatnala Charan kumar
 
Angular Advanced Routing
Angular Advanced RoutingAngular Advanced Routing
Angular Advanced RoutingLaurent Duveau
 

Mais procurados (20)

Angular - Chapter 5 - Directives
 Angular - Chapter 5 - Directives Angular - Chapter 5 - Directives
Angular - Chapter 5 - Directives
 
Angular 10 course_content
Angular 10 course_contentAngular 10 course_content
Angular 10 course_content
 
Angular overview
Angular overviewAngular overview
Angular overview
 
Angular
AngularAngular
Angular
 
Angular
AngularAngular
Angular
 
Angular Directives
Angular DirectivesAngular Directives
Angular Directives
 
Angular - Chapter 4 - Data and Event Handling
 Angular - Chapter 4 - Data and Event Handling Angular - Chapter 4 - Data and Event Handling
Angular - Chapter 4 - Data and Event Handling
 
Angular Data Binding
Angular Data BindingAngular Data Binding
Angular Data Binding
 
AngularJS
AngularJSAngularJS
AngularJS
 
Angular introduction students
Angular introduction studentsAngular introduction students
Angular introduction students
 
REST API and CRUD
REST API and CRUDREST API and CRUD
REST API and CRUD
 
Angular 2.0 forms
Angular 2.0 formsAngular 2.0 forms
Angular 2.0 forms
 
Angular Data Binding
Angular Data BindingAngular Data Binding
Angular Data Binding
 
Angular 5 presentation for beginners
Angular 5 presentation for beginnersAngular 5 presentation for beginners
Angular 5 presentation for beginners
 
Angular modules in depth
Angular modules in depthAngular modules in depth
Angular modules in depth
 
Angular js PPT
Angular js PPTAngular js PPT
Angular js PPT
 
Angular Directives | Angular 2 Custom Directives | Angular Tutorial | Angular...
Angular Directives | Angular 2 Custom Directives | Angular Tutorial | Angular...Angular Directives | Angular 2 Custom Directives | Angular Tutorial | Angular...
Angular Directives | Angular 2 Custom Directives | Angular Tutorial | Angular...
 
Angular
AngularAngular
Angular
 
Angular Interview Questions & Answers
Angular Interview Questions & AnswersAngular Interview Questions & Answers
Angular Interview Questions & Answers
 
Angular Advanced Routing
Angular Advanced RoutingAngular Advanced Routing
Angular Advanced Routing
 

Semelhante a Introduction to AngularJS

Angular js for Beginnners
Angular js for BeginnnersAngular js for Beginnners
Angular js for BeginnnersSantosh Kumar Kar
 
Learning AngularJS - Complete coverage of AngularJS features and concepts
Learning AngularJS  - Complete coverage of AngularJS features and conceptsLearning AngularJS  - Complete coverage of AngularJS features and concepts
Learning AngularJS - Complete coverage of AngularJS features and conceptsSuresh Patidar
 
Angular Js Get Started - Complete Course
Angular Js Get Started - Complete CourseAngular Js Get Started - Complete Course
Angular Js Get Started - Complete CourseEPAM Systems
 
Angularjs
AngularjsAngularjs
AngularjsYnon Perek
 
Dive into AngularJS and directives
Dive into AngularJS and directivesDive into AngularJS and directives
Dive into AngularJS and directivesTricode (part of Dept)
 
Introduction to AngularJS
Introduction to AngularJSIntroduction to AngularJS
Introduction to AngularJSJussi Pohjolainen
 
Basics of AngularJS
Basics of AngularJSBasics of AngularJS
Basics of AngularJSFilip Janevski
 
Intoduction to Angularjs
Intoduction to AngularjsIntoduction to Angularjs
Intoduction to AngularjsGaurav Agrawal
 
Angular js 1.3 presentation for fed nov 2014
Angular js 1.3 presentation for fed   nov 2014Angular js 1.3 presentation for fed   nov 2014
Angular js 1.3 presentation for fed nov 2014Sarah Hudson
 
AgularJS basics- angular directives and controllers
AgularJS basics- angular directives and controllersAgularJS basics- angular directives and controllers
AgularJS basics- angular directives and controllersjobinThomas54
 
AngularJs Workshop SDP December 28th 2014
AngularJs Workshop SDP December 28th 2014AngularJs Workshop SDP December 28th 2014
AngularJs Workshop SDP December 28th 2014Ran Wahle
 
End to-End SPA Development Using ASP.NET and AngularJS
End to-End SPA Development Using ASP.NET and AngularJSEnd to-End SPA Development Using ASP.NET and AngularJS
End to-End SPA Development Using ASP.NET and AngularJSGil Fink
 
angularJs Workshop
angularJs WorkshopangularJs Workshop
angularJs WorkshopRan Wahle
 

Semelhante a Introduction to AngularJS (20)

AngularJS
AngularJSAngularJS
AngularJS
 
Angular js for Beginnners
Angular js for BeginnnersAngular js for Beginnners
Angular js for Beginnners
 
Angular
AngularAngular
Angular
 
Angular
AngularAngular
Angular
 
Angular js
Angular jsAngular js
Angular js
 
Learning AngularJS - Complete coverage of AngularJS features and concepts
Learning AngularJS  - Complete coverage of AngularJS features and conceptsLearning AngularJS  - Complete coverage of AngularJS features and concepts
Learning AngularJS - Complete coverage of AngularJS features and concepts
 
Angular Js Get Started - Complete Course
Angular Js Get Started - Complete CourseAngular Js Get Started - Complete Course
Angular Js Get Started - Complete Course
 
Angularjs
AngularjsAngularjs
Angularjs
 
Dive into AngularJS and directives
Dive into AngularJS and directivesDive into AngularJS and directives
Dive into AngularJS and directives
 
Introduction to AngularJS
Introduction to AngularJSIntroduction to AngularJS
Introduction to AngularJS
 
Basics of AngularJS
Basics of AngularJSBasics of AngularJS
Basics of AngularJS
 
Intoduction to Angularjs
Intoduction to AngularjsIntoduction to Angularjs
Intoduction to Angularjs
 
Angular
AngularAngular
Angular
 
Wt unit 5 client &amp; server side framework
Wt unit 5 client &amp; server side frameworkWt unit 5 client &amp; server side framework
Wt unit 5 client &amp; server side framework
 
Angular js
Angular jsAngular js
Angular js
 
Angular js 1.3 presentation for fed nov 2014
Angular js 1.3 presentation for fed   nov 2014Angular js 1.3 presentation for fed   nov 2014
Angular js 1.3 presentation for fed nov 2014
 
AgularJS basics- angular directives and controllers
AgularJS basics- angular directives and controllersAgularJS basics- angular directives and controllers
AgularJS basics- angular directives and controllers
 
AngularJs Workshop SDP December 28th 2014
AngularJs Workshop SDP December 28th 2014AngularJs Workshop SDP December 28th 2014
AngularJs Workshop SDP December 28th 2014
 
End to-End SPA Development Using ASP.NET and AngularJS
End to-End SPA Development Using ASP.NET and AngularJSEnd to-End SPA Development Using ASP.NET and AngularJS
End to-End SPA Development Using ASP.NET and AngularJS
 
angularJs Workshop
angularJs WorkshopangularJs Workshop
angularJs Workshop
 

Mais de David Parsons

Applying Theories in Mobile Learning Research
Applying Theories in Mobile Learning ResearchApplying Theories in Mobile Learning Research
Applying Theories in Mobile Learning ResearchDavid Parsons
 
Exploring Mobile Affordances in the Digital Classroom
Exploring Mobile Affordances in the Digital ClassroomExploring Mobile Affordances in the Digital Classroom
Exploring Mobile Affordances in the Digital ClassroomDavid Parsons
 
A Brief Guide to Game Engines
A Brief Guide to Game EnginesA Brief Guide to Game Engines
A Brief Guide to Game EnginesDavid Parsons
 
Planning Poker
Planning PokerPlanning Poker
Planning PokerDavid Parsons
 
Creating game like activities in agile software engineering education
Creating game like activities in agile software engineering educationCreating game like activities in agile software engineering education
Creating game like activities in agile software engineering educationDavid Parsons
 
Localizing mobile learning policy for maximum return on investment and stakeh...
Localizing mobile learning policy for maximum return on investment and stakeh...Localizing mobile learning policy for maximum return on investment and stakeh...
Localizing mobile learning policy for maximum return on investment and stakeh...David Parsons
 
Cloud Analytics - Using cloud based services to analyse big data
Cloud Analytics - Using cloud based services to analyse big dataCloud Analytics - Using cloud based services to analyse big data
Cloud Analytics - Using cloud based services to analyse big dataDavid Parsons
 
M learning Devices in Education
M learning Devices in EducationM learning Devices in Education
M learning Devices in EducationDavid Parsons
 
Jam today - Embedding BYOD into Classroom Practice
Jam today - Embedding BYOD into Classroom PracticeJam today - Embedding BYOD into Classroom Practice
Jam today - Embedding BYOD into Classroom PracticeDavid Parsons
 
The Java Story
The Java StoryThe Java Story
The Java StoryDavid Parsons
 
An Introduction to MusicXML
An Introduction to MusicXMLAn Introduction to MusicXML
An Introduction to MusicXMLDavid Parsons
 
Naked Objects and Groovy Grails
Naked Objects and Groovy GrailsNaked Objects and Groovy Grails
Naked Objects and Groovy GrailsDavid Parsons
 
Designing mobile games for engagement and learning
Designing mobile games for engagement and learningDesigning mobile games for engagement and learning
Designing mobile games for engagement and learningDavid Parsons
 
The Impact of Methods and Techniques on Outcomes from Agile Software Developm...
The Impact of Methods and Techniques on Outcomes from Agile Software Developm...The Impact of Methods and Techniques on Outcomes from Agile Software Developm...
The Impact of Methods and Techniques on Outcomes from Agile Software Developm...David Parsons
 
Interaction on the Move
Interaction on the MoveInteraction on the Move
Interaction on the MoveDavid Parsons
 

Mais de David Parsons (15)

Applying Theories in Mobile Learning Research
Applying Theories in Mobile Learning ResearchApplying Theories in Mobile Learning Research
Applying Theories in Mobile Learning Research
 
Exploring Mobile Affordances in the Digital Classroom
Exploring Mobile Affordances in the Digital ClassroomExploring Mobile Affordances in the Digital Classroom
Exploring Mobile Affordances in the Digital Classroom
 
A Brief Guide to Game Engines
A Brief Guide to Game EnginesA Brief Guide to Game Engines
A Brief Guide to Game Engines
 
Planning Poker
Planning PokerPlanning Poker
Planning Poker
 
Creating game like activities in agile software engineering education
Creating game like activities in agile software engineering educationCreating game like activities in agile software engineering education
Creating game like activities in agile software engineering education
 
Localizing mobile learning policy for maximum return on investment and stakeh...
Localizing mobile learning policy for maximum return on investment and stakeh...Localizing mobile learning policy for maximum return on investment and stakeh...
Localizing mobile learning policy for maximum return on investment and stakeh...
 
Cloud Analytics - Using cloud based services to analyse big data
Cloud Analytics - Using cloud based services to analyse big dataCloud Analytics - Using cloud based services to analyse big data
Cloud Analytics - Using cloud based services to analyse big data
 
M learning Devices in Education
M learning Devices in EducationM learning Devices in Education
M learning Devices in Education
 
Jam today - Embedding BYOD into Classroom Practice
Jam today - Embedding BYOD into Classroom PracticeJam today - Embedding BYOD into Classroom Practice
Jam today - Embedding BYOD into Classroom Practice
 
The Java Story
The Java StoryThe Java Story
The Java Story
 
An Introduction to MusicXML
An Introduction to MusicXMLAn Introduction to MusicXML
An Introduction to MusicXML
 
Naked Objects and Groovy Grails
Naked Objects and Groovy GrailsNaked Objects and Groovy Grails
Naked Objects and Groovy Grails
 
Designing mobile games for engagement and learning
Designing mobile games for engagement and learningDesigning mobile games for engagement and learning
Designing mobile games for engagement and learning
 
The Impact of Methods and Techniques on Outcomes from Agile Software Developm...
The Impact of Methods and Techniques on Outcomes from Agile Software Developm...The Impact of Methods and Techniques on Outcomes from Agile Software Developm...
The Impact of Methods and Techniques on Outcomes from Agile Software Developm...
 
Interaction on the Move
Interaction on the MoveInteraction on the Move
Interaction on the Move
 

Último

WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Hararemasabamasaba
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benonimasabamasaba
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxAnnaArtyushina1
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is insideshinachiaurasa2
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareJim McKeeth
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park masabamasaba
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrandmasabamasaba
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park masabamasaba
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024VictoriaMetrics
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech studentsHimanshiGarg82
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...masabamasaba
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfkalichargn70th171
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfonteinmasabamasaba
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Bert Jan Schrijver
 

Último (20)

WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
Abortion Pills In Pretoria ](+27832195400*)[ đŸ„ Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ đŸ„ Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ đŸ„ Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ đŸ„ Women's Abortion Clinic In Pre...
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 

Introduction to AngularJS

  • 1. Associate Professor David Parsons Massey University Auckland, New Zealand
  • 2. Outline ‱ AngularJS components ‱ Directives and Expressions ‱ Data binding ‱ Modules And Controllers
  • 3. AngularJS ‱ A framework rather than a library ‱ Good choice for Single Page App development ‱ Extends HTML by adding new elements and custom attributes that carry special meaning Library App Framework App
  • 4. AngularJS ‱ AngularJS is currently being maintained by developers at Google ‱ Open source software released under the MIT license ‱ Available for download at GitHub ‱ Called AngularJS because HTML uses angle brackets
  • 5. Model-View-Whatever ‱ AngularJS is an MVW framework (Model-View-Whatever) ‱ Can be used to develop apps based on either – MVC (Model-View-Controller) – MVVM (Model-View-ViewModel) ‱ aka naked objects
  • 6. AngularJS Components 1. Model The data shown to the users (JavaScript Objects) 2. View This is what the users see (generated HTML) 3. Controller The business logic behind an application 4. Scope A context that holds data models and functions 5. Directives Extend HTML with custom elements and attributes. 6. Expressions Represented by {{}} in the HTML, can access models and functions from the scope 7. Templates HTML with additional markup in the form of directives and expressions
  • 7. Libraries ‱ The main library is the angular.js file ‱ Download from angularjs.org or use the Google CDN
  • 8. Directives ‱ Angular.js extends HTML with directives ‱ These directives are HTML attributes with an ‘ng’ prefix. – Or ‘data-ng’ in HTML5 ‱ Important directives: – ng-app ‱ defines an AngularJS application – ng-model ‱ binds the value of HTML controls to application data – ng-bind ‱ binds application data to the HTML view
  • 9. The ng-app Directive ‱ This is required for the page to use Angular ‱ Applied to an HTML element ‱ The simplest version does not relate to any external definition – App name quotes are empty <body ng-app=""> .. </body>
  • 10. Expressions ‱ The main purpose of an expression is binding data from the model to the view ‱ The expression is dynamically re-evaluated each time any of the data it depends on changes ‱ Written inside double braces ‱ Result is included in the page where the expression is written ‱ Simple examples: – Arithmetic expressions – String expressions {{ 5 + 4 }} {{ "Hello " + "World" }} {{ expression }}
  • 11. Testing Angular Configuration ‱ A simple expression is a good way of checking that the angular library is found and that you have the required ng-app directive
  • 12. Two-way Data Binding ‱ One important feature of Angular is the way it binds values to expressions ‱ These values can be in the HTML (view) or in JavaScript variables or objects (model) ‱ Data binding is automatic ‱ Angular automatically updates bound values
  • 13. ng-model and ng-bind ‱ An HTML element that contains data can be bound to a value in the model ‱ The innerHTML of another element can be bound to that part of the model <div ng-app=""> <p>Name: <input type="text" ng-model="name"></p> <p ng-bind="name"></p> </div>
  • 14. The ng-init Directive ‱ This directive can be used to initialise values in the model ‱ The ng-init directive can contain multiple initialisations, separated by semicolons <div ng-app="" ng-init="name='Massey' "> <p>Name: <input type="text" ng-model="name"></p> <p ng-bind="name"></p> </div> ng-init="forename='Massey'; lastname='University' "
  • 15. Binding an Expression ‱ The ng-bind directive can contain an expression ‱ In this example it multiplies a data value from the model (“number”) by itself <div ng-app="" ng-init="number=10"> <p>Number: <input type="text" ng-model="number" ></p> <p>Square:</p> <p ng-bind="number * number"> </p> </div>
  • 16. Modules ‱ Angular code in external files is defined in modules ‱ The first parameter is the name of the app module that can be referenced from an ng-app directive – The array is for any dependencies we may have on other modules (can be empty) var app = angular.module("ticketoffice", [ ]); <div ng-app="ticketoffice">
  • 17. Controllers ‱ Apps have controllers ‱ These are JavaScript functions ‱ Given a name when added to an app as a controller ‱ Name your controllers using Pascal Case – Controllers are really constructor functions – These are usually named in JavaScript using Pascal case var app=angular.module("ticketoffice", []); app.controller("TicketController", function() { // body of function });
  • 18. The ng-controller Directive ‱ Angular uses the ng-controller directive to call controller functions ‱ Here, TicketController shows an alert (just as an example, to show it is being called) app.controller("TicketController", function(){ alert("TicketController called"); }); <div ng-controller="TicketController"> </div>
  • 19. Object Data in the Controller ‱ The controller might access object data ‱ In this case a travel ticket (‘traveldoc’) app.controller("TicketController", function(){ this.traveldoc=ticket; }); var ticket= { origin : "Wellington", destination : "Auckland", price : 110 }
  • 20. Controller Alias ‱ To access data from the controller, we can use an alias ‱ Inside the div, the alias can be used to access the data from the controller, using expressions – Note: ‱ This controller’s scope is within the div only ‱ Sometimes will need a broader scope <div ng-controller="TicketController as agent"> <h2>Origin: {{agent.traveldoc.origin}}</h2> <h2>Destination: {{agent.traveldoc.destination}}</h2> <h3>Price: ${{agent.traveldoc.price}}</h3>
  • 21. Multiple Objects ‱ We might have an array of objects ‱ We also need to change the controller, since the name has changed, for readability (from ‘ticket’ to ‘tickets’) var tickets = [ { origin : "Wellington", destination : "Auckland", price : 110}, { origin : "Christchurch", destination : "Dundedin", price : 120}, 
 ]; this.traveldocs=tickets;
  • 22. Array Access by Index ‱ Access by index is now possible, e.g. ‱ However, not good for displaying multiple objects on the same page <h2>{{agent.traveldocs[0].origin}}</h2> <h2>{{agent.traveldocs[0].destination}}</h2> <h3>${{agent.traveldocs[0].price}}</h3>
  • 23. The ng-repeat Directive ‱ Can be used to iterate over an array of objects ‱ Note the ‘in’ ‱ The controller reference is moved to the enclosing scope <body ng-controller="TicketController as agent" > <div ng-repeat="traveldoc in agent.traveldocs"> <h2>{{traveldoc.origin}}</h2> <h2>{{traveldoc.destination}}</h2> <h3>${{traveldoc.price}}</h3>
  • 24. Adding Images with ng-src ‱ When adding images with Angular, we need to use the ng-src directive, instead of the standard ‘src’ attribute of the ‘img’ tag ‱ Let’s assume our ticket objects each include an array of images: var tickets = [ { origin : "Wellington", destination : "Auckland", price : 110, isAvailable : false, images: [ { full : "wellington1-full.jpg", thumb : "wellington1-thumb.jpg" }, { full : "wellington2-full.jpg", thumb : "wellington2-thumb.jpg" }, 

  • 25. Using ng-src ‱ In the HTML img tag, replace ‘src’ with ‘ng-src’, along with an Angular expression to locate the image. <img ng-src="{{traveldoc.images[0].full}}" />
  • 26. Directive Summary ‱ Here is summary of the directives that we have seen so far: – ng-app – ng-model – ng-init – ng-bind – ng-controller – ng-src – ng-repeat