SlideShare a Scribd company logo
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

More Related Content

What's hot (20)

PPTX
Angularjs PPT
Amit Baghel
 
PDF
JavaScript - Chapter 4 - Types and Statements
WebStackAcademy
 
PDF
Angular - Chapter 3 - Components
WebStackAcademy
 
PPTX
Introduction to spring boot
Santosh Kumar Kar
 
PDF
JavaScript Programming
Sehwan Noh
 
PPT
jQuery
Mostafa Bayomi
 
PPTX
Spring Boot
Jiayun Zhou
 
PPTX
Angular 14.pptx
MohaNedGhawar
 
PPTX
Angular
Mouad EL Fakir
 
PPT
Angular 8
Sunil OS
 
PPTX
Angular modules in depth
Christoffer Noring
 
PDF
Angular Directives
iFour Technolab Pvt. Ltd.
 
PPTX
Typescript ppt
akhilsreyas
 
PDF
Javascript essentials
Bedis ElAchèche
 
PDF
Spring Framework - Core
Dzmitry Naskou
 
PDF
Lets make a better react form
Yao Nien Chung
 
PPT
Java Script ppt
Priya Goyal
 
PDF
Basics of JavaScript
Bala Narayanan
 
Angularjs PPT
Amit Baghel
 
JavaScript - Chapter 4 - Types and Statements
WebStackAcademy
 
Angular - Chapter 3 - Components
WebStackAcademy
 
Introduction to spring boot
Santosh Kumar Kar
 
JavaScript Programming
Sehwan Noh
 
Spring Boot
Jiayun Zhou
 
Angular 14.pptx
MohaNedGhawar
 
Angular 8
Sunil OS
 
Angular modules in depth
Christoffer Noring
 
Angular Directives
iFour Technolab Pvt. Ltd.
 
Typescript ppt
akhilsreyas
 
Javascript essentials
Bedis ElAchèche
 
Spring Framework - Core
Dzmitry Naskou
 
Lets make a better react form
Yao Nien Chung
 
Java Script ppt
Priya Goyal
 
Basics of JavaScript
Bala Narayanan
 

Similar to Introduction to AngularJS (20)

PDF
Dive into AngularJS and directives
Tricode (part of Dept)
 
PPTX
ANGULARJS introduction components services and directives
SanthoshB77
 
PDF
Wt unit 5 client &amp; server side framework
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
PPTX
Angular workshop - Full Development Guide
Nitin Giri
 
PPTX
Angular Presentation
Adam Moore
 
PPSX
Angular js
Arun Somu Panneerselvam
 
PPTX
Angular js
ParmarAnisha
 
PDF
Workshop 12: AngularJS Parte I
Visual Engineering
 
PPTX
Dive into Angular, part 1: Introduction
Oleksii Prohonnyi
 
PPTX
AngularJS
LearningTech
 
PPTX
Angular Js Get Started - Complete Course
EPAM Systems
 
PPTX
Angular js 1.3 presentation for fed nov 2014
Sarah Hudson
 
PPTX
Angularjs
Sabin Tamrakar
 
PPTX
Basics of AngularJS
Filip Janevski
 
PPTX
AngularJS Introduction, how to run Angular
SamuelJohnpeter
 
PPT
introduction to Angularjs basics
Ravindra K
 
PPTX
Intoduction to Angularjs
Gaurav Agrawal
 
DOCX
Angular js
prasaddammalapati
 
PPTX
AngularJs
syam kumar kk
 
Dive into AngularJS and directives
Tricode (part of Dept)
 
ANGULARJS introduction components services and directives
SanthoshB77
 
Wt unit 5 client &amp; server side framework
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
Angular workshop - Full Development Guide
Nitin Giri
 
Angular Presentation
Adam Moore
 
Angular js
ParmarAnisha
 
Workshop 12: AngularJS Parte I
Visual Engineering
 
Dive into Angular, part 1: Introduction
Oleksii Prohonnyi
 
AngularJS
LearningTech
 
Angular Js Get Started - Complete Course
EPAM Systems
 
Angular js 1.3 presentation for fed nov 2014
Sarah Hudson
 
Angularjs
Sabin Tamrakar
 
Basics of AngularJS
Filip Janevski
 
AngularJS Introduction, how to run Angular
SamuelJohnpeter
 
introduction to Angularjs basics
Ravindra K
 
Intoduction to Angularjs
Gaurav Agrawal
 
Angular js
prasaddammalapati
 
AngularJs
syam kumar kk
 
Ad

More from David Parsons (15)

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

Recently uploaded (20)

PPTX
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
PPTX
An Introduction to ZAP by Checkmarx - Official Version
Simon Bennetts
 
PDF
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
PPTX
The Role of a PHP Development Company in Modern Web Development
SEO Company for School in Delhi NCR
 
PDF
Powering GIS with FME and VertiGIS - Peak of Data & AI 2025
Safe Software
 
PPTX
Human Resources Information System (HRIS)
Amity University, Patna
 
PDF
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
PDF
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
PDF
Alexander Marshalov - How to use AI Assistants with your Monitoring system Q2...
VictoriaMetrics
 
PPTX
Tally software_Introduction_Presentation
AditiBansal54083
 
PDF
Beyond Binaries: Understanding Diversity and Allyship in a Global Workplace -...
Imma Valls Bernaus
 
PPTX
How Apagen Empowered an EPC Company with Engineering ERP Software
SatishKumar2651
 
PPTX
MiniTool Power Data Recovery Full Crack Latest 2025
muhammadgurbazkhan
 
PDF
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
PPTX
Platform for Enterprise Solution - Java EE5
abhishekoza1981
 
PDF
Capcut Pro Crack For PC Latest Version {Fully Unlocked} 2025
hashhshs786
 
PDF
Thread In Android-Mastering Concurrency for Responsive Apps.pdf
Nabin Dhakal
 
PPTX
Engineering the Java Web Application (MVC)
abhishekoza1981
 
PDF
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pdf
Varsha Nayak
 
PPTX
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
An Introduction to ZAP by Checkmarx - Official Version
Simon Bennetts
 
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
The Role of a PHP Development Company in Modern Web Development
SEO Company for School in Delhi NCR
 
Powering GIS with FME and VertiGIS - Peak of Data & AI 2025
Safe Software
 
Human Resources Information System (HRIS)
Amity University, Patna
 
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
Alexander Marshalov - How to use AI Assistants with your Monitoring system Q2...
VictoriaMetrics
 
Tally software_Introduction_Presentation
AditiBansal54083
 
Beyond Binaries: Understanding Diversity and Allyship in a Global Workplace -...
Imma Valls Bernaus
 
How Apagen Empowered an EPC Company with Engineering ERP Software
SatishKumar2651
 
MiniTool Power Data Recovery Full Crack Latest 2025
muhammadgurbazkhan
 
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
Platform for Enterprise Solution - Java EE5
abhishekoza1981
 
Capcut Pro Crack For PC Latest Version {Fully Unlocked} 2025
hashhshs786
 
Thread In Android-Mastering Concurrency for Responsive Apps.pdf
Nabin Dhakal
 
Engineering the Java Web Application (MVC)
abhishekoza1981
 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pdf
Varsha Nayak
 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 

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