SlideShare a Scribd company logo
1 of 33
Download to read offline
AngularJS - Part I
Cristina Hernández
Roger Vilà
Index
● What is AngularJS? Why Angular JS?
● History of AngularJS
● Angular JS Patterns
● Introduce to AngularJS Code
● Invoking Angular JS
● DataBinding in AngularJS
● Expressions
● Directives
● Filters
● Modules
● Controllers
● Services
● Forms
● Application example
“Angularjs is what HTML would have been if it had
been designed for building web applications”
https://angularjs.org/
What is Angular JS?
AngularJS is an open-source web application framework for
client-side MVC and MVVM architectures.
Why Angular JS?
HTML is great for declaring static documents, but it falters
when we try to use it for declaring dynamic views in web-
applications. Angular JS lets you extend HTML vocabulary for
your application
History of Angular JS
AngularJS was originally developed in
2009 by Misko Hevery at Brach Tech LLC.
Created at Google as internal project.
AngularJS version 1.0 was released in 2012.
Currently working on 1.4.9
Angular JS 2 (Beta)
https://en.wikipedia.org/wiki/AngularJS
MVC - Model View Controller
VIEW
MODEL CONTROLLER
- Renders the Model data
- Sends user actions/events
to controller
- UI
- Defines application
behaviour
- Maps user actions to
Model
- Selects view for response
- Business Logic
- Notifies view changes
- Data in general
User Action or View Load
Maps to particular
Model after fetching
the data
Implements the Business
Logic on response data
and binds it to View
MVVM - Model View View Model
Model
Entire model contained in a single javascript data
structure.
var obj = {
employeeName: "Mattias",
company: "Net Insight AB"
}
Controller
Javascript code that populates the view and reacts to
changes in it.
function myCtrl( $scope ) {
$scope = {
employeeName: "Mattias",
company: "Net Insight AB"
};
$scope.save_info = function() {
console.log( $scope.employeeName );
};
}
View
"Extended" html. Dynamic and syncronized
<div ng-controller = myCtrl>
<h2>{{company}}</h2>
Employee name:
<input ng-model="employeeName"></input>
<button ng-click="save_info()"
>Submit</button>
</div>
Invoking Angular
Any application must do two things to start Angular:
1) Load the angular.js library
2) Tell Angular which part of the DOM it should manage with the ng-app
directive
<script type=”text/javascript” src=”angular.min.js” />
<html>
<div ng-app>
…
</div>
...
</html>
<html ng-app>
....
</html>
Client-Side templates
controllers.js:
function HelloController($scope) {
$scope.greeting = { text: 'Hello' };
}
hello.tml:
<html ng-app>
<head>
<script src="angular.js"></script>
<script src="controllers.js"></script>
</head>
<body>
<div ng-controller='HelloController'>
<p>{{greeting.text}}, World</p>
</div>
</body>
</html>
Result:
Hello, World
Data Binding
-
One-way binding:
Binding from scope to HTML. “Mustache” syntax
{{dato}}
Two-way binding:
Binding from scopel to HTML, and binding from HTML to scope
<input type="text" ng-model="miDato" />
Two-Way Data Binding
hello.html
<html ng-app>
<head>
<script src="angular.js"></script>
<script src="controllers.js"></script>
</head>
<body>
<div ng-controller='HelloController'>
<input ng-model='greeting.text'>
<p>{{greeting.text}}, World</p>
</div>
</body>
</html>
controllers.js:
function HelloController($scope) {
$scope.greeting = { text: 'Hello' };
}
Hello
Hello, World https://docs.angularjs.org/guide/databinding
Expressions
Allow you to insert dynamic values into your HTML
AngularJS expressions can be written inside double braces: {{expression}}
Angular JS expressions can be written inside a directive: ng-bind=”expression”
1) Numerical Operations
{{ 4 + 6}} → 10
2) String Operations
{{“hello” + “ you”}} → hello you
https://docs.angularjs.org/guide/expression
Directives
Directives are ways to expand our html. They allow you to add from small pieces of
code or full functionality
Angular comes with a set of these directives built in, like:
● ng-app
● ng-controller
● ng-model
● ng-bind
● ng-repeat
● ng-init
● ng-show/ng-hide
● ng-class
● ng-click
● ng-form
● ng-submit
● ng-if
● ng-href
● ng-src
● custom directives (Next workshop)
https://docs.angularjs.org/api/ng/directive/
ng-model
With the ng-model directive you can bind the value of an input field to a
variable created in AngularJS
<div ng-app="myApp" ng-controller="myCtrl">
Name: <input ng-model="name">
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.name = "John Doe";
});
</script>
ng-repeat
<h3>FIFA Golden Ball:</h3>
<div ng-app ng-controller="MyCtrl">
<ul>
<li ng-repeat="player in players">{{name}}: {{num}}</li>
</ul>
</div>
function MyCtrl($scope) {
$scope.players = [
{
name: “Roger”
num: 6
},
{
name: “Messi”
num: 5
},
{
name: “Cristiano Ronaldo”
num: 3
}
];
}
ng-show / ng-hide
The ng-show / hide directive shows or hides the given html based
on the expression provided in the ng show/hide attribute
Click me: <input type="checkbox" ng-model="checked">
<div>
Show:
<div class="animate-show" ng-show="checked">
<span class="glyphicon glyphicon-thumbs-up"></span> I show up when your checkbox is checked.
</div>
</div>
<div>
Hide:
<div class="animate-show" ng-hide="checked">
<span class="glyphicon glyphicon-thumbs-down"></span> I hide when your checkbox is checked.
</div>
</div>
Filters
● Angular filters format data for display to the user.
{{ expression [| filter_name[:parameter_value]...] }}
{{ totalCost | currency }}
● Filters can be chained and can take optional
arguments.
{{ totalCost | currency | filter2 | filter3 }}
{{ totalCost | currency:”USD$” }}
Built-in Filters
● filter
● currency
● number
● date
● json
● lowercase
● uppercase
● limitTo
● orderBy
https://docs.angularjs.org/api/ng/filter
Filters
<div ng-init="friends = [{name:'John', phone:'555-1276'},
{name:'Mary', phone:'800-BIG-MARY'},
{name:'Mike', phone:'555-4321'}]"></div>
<label>Search: <input ng-model="searchText"></label>
<table id="searchTextResults">
<tr><th>Name</th><th>Phone</th></tr>
<tr ng-repeat="friend in friends | filter:searchText |
orderBy:'name'">
<td>{{friend.name}}</td>
<td>{{friend.phone}}</td>
</tr>
</table>
Filters in Javascript
$filter(‘number’)(15,5) {{ 15 | number:5 }}
Modules
- Where we write pieces of our Angular application.
- Makes our code more maintainable, testable and
readable.
- Where we define dependencies for our app
Modules
var app = angular.module(‘moduleName’, [] );
Dependencies
Creating:
Including the module:
<html ng-app=”moduleName”>
....
</html>
<html>
<div ng-app=”moduleName”>
…
</div>
...
</html>
Modules
var app = angular.module(‘moduleName’, [] )
.config(function () {
...
}).run(function () {
...
});
app.controller(function(){
…
});
app.service(function(){
…
});
app.directive(function(){
…
});
Controllers
Controllers are where we define our app’s behavior by
defining functions and values.
app.controller('ViewCtrl', ['$scope', '$location', 'recipe',
function($scope, $location, recipe) {
$scope.recipe = recipe;
$scope.edit = function() {
$location.path('/edit/' + recipe.id);
};
}]);
<html>
<div ng-controller=·ViewCtrl”>
…
</div>
...
</html>
$scope
Scope is an object that refers to the application
model.
VIEW CONTROLLER$scope
<input type=”text” ng-model=”name” />
function SimpleController($scope) {
$scope.name = “Leo”;
}
Services
Services are singletons objects that are instantiated
only once per app.
Services are used to organize and share code
across your app.
Controllers are view-specific, services are app-
specific.
Built-in Services
● $http
● $q
● $window
● $location
● ...
Built-in Services - $http
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readystate == 4 && xmlhttp.status == 200) {
var response = xmlhttp.responseText;
} else if (xmlhttp.status == 400) {
// Handle error gracefully
}
};
// Setup connection
xmlhttp.open(“GET”, “http://myserver/api”, true);
// Make the request
xmlhttp.send();
$http.get('api/user', {params: {id: '5'}
}).success(function(data, status,
headers, config) {
// Do something successful.
}).error(function(data, status, headers,
config) {
// Handle the error
});
Forms
Forms and controls provide validation services, so that
the user can be notified of invalid input.
This provides a better user experience.
<div ng-form=”loginForm” >
<input type=”text” ng-model=”user.name” name=”uName” required />
<button ng-click=”update(user)” ng-disabled=”loginForm.$invalid”>
SAVE
</button>
</div>
Debugging
Batarang → Google Chrome Extension

More Related Content

What's hot

Practical AngularJS
Practical AngularJSPractical AngularJS
Practical AngularJS
Wei Ru
 

What's hot (20)

Practical AngularJS
Practical AngularJSPractical AngularJS
Practical AngularJS
 
AngularJS Internal
AngularJS InternalAngularJS Internal
AngularJS Internal
 
AngularJS Directives
AngularJS DirectivesAngularJS Directives
AngularJS Directives
 
Angularjs Basics
Angularjs BasicsAngularjs Basics
Angularjs Basics
 
The AngularJS way
The AngularJS wayThe AngularJS way
The AngularJS way
 
Sane Async Patterns
Sane Async PatternsSane Async Patterns
Sane Async Patterns
 
AngularJS Architecture
AngularJS ArchitectureAngularJS Architecture
AngularJS Architecture
 
Angular js routing options
Angular js routing optionsAngular js routing options
Angular js routing options
 
AngularJS Framework
AngularJS FrameworkAngularJS Framework
AngularJS Framework
 
IndexedDB - Querying and Performance
IndexedDB - Querying and PerformanceIndexedDB - Querying and Performance
IndexedDB - Querying and Performance
 
AngularJs Crash Course
AngularJs Crash CourseAngularJs Crash Course
AngularJs Crash Course
 
Advanced Tips & Tricks for using Angular JS
Advanced Tips & Tricks for using Angular JSAdvanced Tips & Tricks for using Angular JS
Advanced Tips & Tricks for using Angular JS
 
Workshop 9: BackboneJS y patrones MVC
Workshop 9: BackboneJS y patrones MVCWorkshop 9: BackboneJS y patrones MVC
Workshop 9: BackboneJS y patrones MVC
 
Introduction to AJAX In WordPress
Introduction to AJAX In WordPressIntroduction to AJAX In WordPress
Introduction to AJAX In WordPress
 
Workshop Intro: FrontEnd General Overview
Workshop Intro: FrontEnd General OverviewWorkshop Intro: FrontEnd General Overview
Workshop Intro: FrontEnd General Overview
 
Dynamic Application Development by NodeJS ,AngularJS with OrientDB
Dynamic Application Development by NodeJS ,AngularJS with OrientDBDynamic Application Development by NodeJS ,AngularJS with OrientDB
Dynamic Application Development by NodeJS ,AngularJS with OrientDB
 
Angular js
Angular jsAngular js
Angular js
 
Workshop 2: JavaScript Design Patterns
Workshop 2: JavaScript Design PatternsWorkshop 2: JavaScript Design Patterns
Workshop 2: JavaScript Design Patterns
 
Introduction to AngularJS
Introduction to AngularJSIntroduction to AngularJS
Introduction to AngularJS
 
AngularJS Basic Training
AngularJS Basic TrainingAngularJS Basic Training
AngularJS Basic Training
 

Viewers also liked

Viewers also liked (20)

Workshop 10: ECMAScript 6
Workshop 10: ECMAScript 6Workshop 10: ECMAScript 6
Workshop 10: ECMAScript 6
 
Workshop 5: JavaScript testing
Workshop 5: JavaScript testingWorkshop 5: JavaScript testing
Workshop 5: JavaScript testing
 
Workshop 8: Templating: Handlebars, DustJS
Workshop 8: Templating: Handlebars, DustJSWorkshop 8: Templating: Handlebars, DustJS
Workshop 8: Templating: Handlebars, DustJS
 
Beginner workshop to angularjs presentation at Google
Beginner workshop to angularjs presentation at GoogleBeginner workshop to angularjs presentation at Google
Beginner workshop to angularjs presentation at Google
 
Workshop 15: Ionic framework
Workshop 15: Ionic frameworkWorkshop 15: Ionic framework
Workshop 15: Ionic framework
 
Workshop 7: Single Page Applications
Workshop 7: Single Page ApplicationsWorkshop 7: Single Page Applications
Workshop 7: Single Page Applications
 
Unlock The Value Of Your Microsoft and SAP Investments
Unlock The Value Of Your Microsoft and SAP InvestmentsUnlock The Value Of Your Microsoft and SAP Investments
Unlock The Value Of Your Microsoft and SAP Investments
 
Change document display
Change document displayChange document display
Change document display
 
Workshop iOS 3: Testing, protocolos y extensiones
Workshop iOS 3: Testing, protocolos y extensionesWorkshop iOS 3: Testing, protocolos y extensiones
Workshop iOS 3: Testing, protocolos y extensiones
 
CDS Unit Testing
CDS Unit TestingCDS Unit Testing
CDS Unit Testing
 
Workshop 11: Trendy web designs & prototyping
Workshop 11: Trendy web designs & prototypingWorkshop 11: Trendy web designs & prototyping
Workshop 11: Trendy web designs & prototyping
 
Workshop iOS 4: Closures, generics & operators
Workshop iOS 4: Closures, generics & operatorsWorkshop iOS 4: Closures, generics & operators
Workshop iOS 4: Closures, generics & operators
 
Multithreading 101
Multithreading 101Multithreading 101
Multithreading 101
 
Hana sql
Hana sql Hana sql
Hana sql
 
JavaScript for ABAP Programmers - 7/7 Functional Programming
JavaScript for ABAP Programmers - 7/7 Functional ProgrammingJavaScript for ABAP Programmers - 7/7 Functional Programming
JavaScript for ABAP Programmers - 7/7 Functional Programming
 
Automated Testing Of Web Applications Using XML
Automated  Testing Of  Web  Applications Using  XMLAutomated  Testing Of  Web  Applications Using  XML
Automated Testing Of Web Applications Using XML
 
Getting Started with OpenUI5 (San Francisco State University)
Getting Started with OpenUI5 (San Francisco State University)Getting Started with OpenUI5 (San Francisco State University)
Getting Started with OpenUI5 (San Francisco State University)
 
Python Intro
Python IntroPython Intro
Python Intro
 
Introduction to Design Thinking
Introduction to Design ThinkingIntroduction to Design Thinking
Introduction to Design Thinking
 
Workhop iOS 1: Fundamentos de Swift
Workhop iOS 1: Fundamentos de SwiftWorkhop iOS 1: Fundamentos de Swift
Workhop iOS 1: Fundamentos de Swift
 

Similar to Workshop 12: AngularJS Parte I

Similar to Workshop 12: AngularJS Parte I (20)

Dive into AngularJS and directives
Dive into AngularJS and directivesDive into AngularJS and directives
Dive into AngularJS and directives
 
The Basics Angular JS
The Basics Angular JS The Basics Angular JS
The Basics Angular JS
 
Introduction to AngularJS By Bharat Makwana
Introduction to AngularJS By Bharat MakwanaIntroduction to AngularJS By Bharat Makwana
Introduction to AngularJS By Bharat Makwana
 
Angular js
Angular jsAngular js
Angular js
 
Angular Js Get Started - Complete Course
Angular Js Get Started - Complete CourseAngular Js Get Started - Complete Course
Angular Js Get Started - Complete Course
 
AngularJS training - Day 1 - Basics: Why, What and basic features of AngularJS
AngularJS training - Day 1 - Basics: Why, What and basic features of AngularJSAngularJS training - Day 1 - Basics: Why, What and basic features of AngularJS
AngularJS training - Day 1 - Basics: Why, What and basic features of AngularJS
 
AngularJS 101 - Everything you need to know to get started
AngularJS 101 - Everything you need to know to get startedAngularJS 101 - Everything you need to know to get started
AngularJS 101 - Everything you need to know to get started
 
Basics of AngularJS
Basics of AngularJSBasics of AngularJS
Basics of AngularJS
 
AngularJs Superheroic JavaScript MVW Framework Services by Miracle Studios
AngularJs Superheroic JavaScript MVW Framework Services by Miracle StudiosAngularJs Superheroic JavaScript MVW Framework Services by Miracle Studios
AngularJs Superheroic JavaScript MVW Framework Services by Miracle Studios
 
Angular Js Basics
Angular Js BasicsAngular Js Basics
Angular Js Basics
 
AngularJS: an introduction
AngularJS: an introductionAngularJS: an introduction
AngularJS: an introduction
 
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
 
Introduction to AngularJS
Introduction to AngularJSIntroduction to AngularJS
Introduction to AngularJS
 
Angular js slides
Angular js slidesAngular js slides
Angular js slides
 
Introduction to single page application with angular js
Introduction to single page application with angular jsIntroduction to single page application with angular js
Introduction to single page application with angular js
 
Angular js
Angular jsAngular js
Angular js
 
Angular js
Angular jsAngular js
Angular js
 
AngularJS By Vipin
AngularJS By VipinAngularJS By Vipin
AngularJS By Vipin
 
Angular js
Angular jsAngular js
Angular js
 
Built in filters
Built in filtersBuilt in filters
Built in filters
 

More from Visual Engineering

More from Visual Engineering (14)

Workshop iOS 2: Swift - Structures
Workshop iOS 2: Swift - StructuresWorkshop iOS 2: Swift - Structures
Workshop iOS 2: Swift - Structures
 
Workshop 26: React Native - The Native Side
Workshop 26: React Native - The Native SideWorkshop 26: React Native - The Native Side
Workshop 26: React Native - The Native Side
 
Workshop 25: React Native - Components
Workshop 25: React Native - ComponentsWorkshop 25: React Native - Components
Workshop 25: React Native - Components
 
Workshop 23: ReactJS, React & Redux testing
Workshop 23: ReactJS, React & Redux testingWorkshop 23: ReactJS, React & Redux testing
Workshop 23: ReactJS, React & Redux testing
 
Workshop 22: ReactJS Redux Advanced
Workshop 22: ReactJS Redux AdvancedWorkshop 22: ReactJS Redux Advanced
Workshop 22: ReactJS Redux Advanced
 
Workshop 22: React-Redux Middleware
Workshop 22: React-Redux MiddlewareWorkshop 22: React-Redux Middleware
Workshop 22: React-Redux Middleware
 
Workshop 21: React Router
Workshop 21: React RouterWorkshop 21: React Router
Workshop 21: React Router
 
Workshop 20: ReactJS Part II Flux Pattern & Redux
Workshop 20: ReactJS Part II Flux Pattern & ReduxWorkshop 20: ReactJS Part II Flux Pattern & Redux
Workshop 20: ReactJS Part II Flux Pattern & Redux
 
Workshop 19: ReactJS Introduction
Workshop 19: ReactJS IntroductionWorkshop 19: ReactJS Introduction
Workshop 19: ReactJS Introduction
 
Workshop 18: CSS Animations & cool effects
Workshop 18: CSS Animations & cool effectsWorkshop 18: CSS Animations & cool effects
Workshop 18: CSS Animations & cool effects
 
Workshop 17: EmberJS parte II
Workshop 17: EmberJS parte IIWorkshop 17: EmberJS parte II
Workshop 17: EmberJS parte II
 
Workshop 6: Designer tools
Workshop 6: Designer toolsWorkshop 6: Designer tools
Workshop 6: Designer tools
 
Workshop 4: NodeJS. Express Framework & MongoDB.
Workshop 4: NodeJS. Express Framework & MongoDB.Workshop 4: NodeJS. Express Framework & MongoDB.
Workshop 4: NodeJS. Express Framework & MongoDB.
 
Workshop 3: JavaScript build tools
Workshop 3: JavaScript build toolsWorkshop 3: JavaScript build tools
Workshop 3: JavaScript build tools
 

Recently uploaded

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
VictoriaMetrics
 

Recently uploaded (20)

WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
 
%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
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
 
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
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 
%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
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
 
%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
 
%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
 
%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
 
WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaS
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
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
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
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
 

Workshop 12: AngularJS Parte I

  • 1. AngularJS - Part I Cristina Hernández Roger Vilà
  • 2. Index ● What is AngularJS? Why Angular JS? ● History of AngularJS ● Angular JS Patterns ● Introduce to AngularJS Code ● Invoking Angular JS ● DataBinding in AngularJS ● Expressions ● Directives ● Filters ● Modules ● Controllers ● Services ● Forms ● Application example
  • 3. “Angularjs is what HTML would have been if it had been designed for building web applications” https://angularjs.org/
  • 4. What is Angular JS? AngularJS is an open-source web application framework for client-side MVC and MVVM architectures. Why Angular JS? HTML is great for declaring static documents, but it falters when we try to use it for declaring dynamic views in web- applications. Angular JS lets you extend HTML vocabulary for your application
  • 5. History of Angular JS AngularJS was originally developed in 2009 by Misko Hevery at Brach Tech LLC. Created at Google as internal project. AngularJS version 1.0 was released in 2012. Currently working on 1.4.9 Angular JS 2 (Beta) https://en.wikipedia.org/wiki/AngularJS
  • 6. MVC - Model View Controller VIEW MODEL CONTROLLER - Renders the Model data - Sends user actions/events to controller - UI - Defines application behaviour - Maps user actions to Model - Selects view for response - Business Logic - Notifies view changes - Data in general User Action or View Load Maps to particular Model after fetching the data Implements the Business Logic on response data and binds it to View
  • 7. MVVM - Model View View Model
  • 8. Model Entire model contained in a single javascript data structure. var obj = { employeeName: "Mattias", company: "Net Insight AB" }
  • 9. Controller Javascript code that populates the view and reacts to changes in it. function myCtrl( $scope ) { $scope = { employeeName: "Mattias", company: "Net Insight AB" }; $scope.save_info = function() { console.log( $scope.employeeName ); }; }
  • 10. View "Extended" html. Dynamic and syncronized <div ng-controller = myCtrl> <h2>{{company}}</h2> Employee name: <input ng-model="employeeName"></input> <button ng-click="save_info()" >Submit</button> </div>
  • 11. Invoking Angular Any application must do two things to start Angular: 1) Load the angular.js library 2) Tell Angular which part of the DOM it should manage with the ng-app directive <script type=”text/javascript” src=”angular.min.js” /> <html> <div ng-app> … </div> ... </html> <html ng-app> .... </html>
  • 12. Client-Side templates controllers.js: function HelloController($scope) { $scope.greeting = { text: 'Hello' }; } hello.tml: <html ng-app> <head> <script src="angular.js"></script> <script src="controllers.js"></script> </head> <body> <div ng-controller='HelloController'> <p>{{greeting.text}}, World</p> </div> </body> </html> Result: Hello, World
  • 13. Data Binding - One-way binding: Binding from scope to HTML. “Mustache” syntax {{dato}} Two-way binding: Binding from scopel to HTML, and binding from HTML to scope <input type="text" ng-model="miDato" />
  • 14. Two-Way Data Binding hello.html <html ng-app> <head> <script src="angular.js"></script> <script src="controllers.js"></script> </head> <body> <div ng-controller='HelloController'> <input ng-model='greeting.text'> <p>{{greeting.text}}, World</p> </div> </body> </html> controllers.js: function HelloController($scope) { $scope.greeting = { text: 'Hello' }; } Hello Hello, World https://docs.angularjs.org/guide/databinding
  • 15. Expressions Allow you to insert dynamic values into your HTML AngularJS expressions can be written inside double braces: {{expression}} Angular JS expressions can be written inside a directive: ng-bind=”expression” 1) Numerical Operations {{ 4 + 6}} → 10 2) String Operations {{“hello” + “ you”}} → hello you https://docs.angularjs.org/guide/expression
  • 16. Directives Directives are ways to expand our html. They allow you to add from small pieces of code or full functionality Angular comes with a set of these directives built in, like: ● ng-app ● ng-controller ● ng-model ● ng-bind ● ng-repeat ● ng-init ● ng-show/ng-hide ● ng-class ● ng-click ● ng-form ● ng-submit ● ng-if ● ng-href ● ng-src ● custom directives (Next workshop) https://docs.angularjs.org/api/ng/directive/
  • 17. ng-model With the ng-model directive you can bind the value of an input field to a variable created in AngularJS <div ng-app="myApp" ng-controller="myCtrl"> Name: <input ng-model="name"> </div> <script> var app = angular.module('myApp', []); app.controller('myCtrl', function($scope) { $scope.name = "John Doe"; }); </script>
  • 18. ng-repeat <h3>FIFA Golden Ball:</h3> <div ng-app ng-controller="MyCtrl"> <ul> <li ng-repeat="player in players">{{name}}: {{num}}</li> </ul> </div> function MyCtrl($scope) { $scope.players = [ { name: “Roger” num: 6 }, { name: “Messi” num: 5 }, { name: “Cristiano Ronaldo” num: 3 } ]; }
  • 19. ng-show / ng-hide The ng-show / hide directive shows or hides the given html based on the expression provided in the ng show/hide attribute Click me: <input type="checkbox" ng-model="checked"> <div> Show: <div class="animate-show" ng-show="checked"> <span class="glyphicon glyphicon-thumbs-up"></span> I show up when your checkbox is checked. </div> </div> <div> Hide: <div class="animate-show" ng-hide="checked"> <span class="glyphicon glyphicon-thumbs-down"></span> I hide when your checkbox is checked. </div> </div>
  • 20. Filters ● Angular filters format data for display to the user. {{ expression [| filter_name[:parameter_value]...] }} {{ totalCost | currency }} ● Filters can be chained and can take optional arguments. {{ totalCost | currency | filter2 | filter3 }} {{ totalCost | currency:”USD$” }}
  • 21. Built-in Filters ● filter ● currency ● number ● date ● json ● lowercase ● uppercase ● limitTo ● orderBy https://docs.angularjs.org/api/ng/filter
  • 22. Filters <div ng-init="friends = [{name:'John', phone:'555-1276'}, {name:'Mary', phone:'800-BIG-MARY'}, {name:'Mike', phone:'555-4321'}]"></div> <label>Search: <input ng-model="searchText"></label> <table id="searchTextResults"> <tr><th>Name</th><th>Phone</th></tr> <tr ng-repeat="friend in friends | filter:searchText | orderBy:'name'"> <td>{{friend.name}}</td> <td>{{friend.phone}}</td> </tr> </table>
  • 24. Modules - Where we write pieces of our Angular application. - Makes our code more maintainable, testable and readable. - Where we define dependencies for our app
  • 25. Modules var app = angular.module(‘moduleName’, [] ); Dependencies Creating: Including the module: <html ng-app=”moduleName”> .... </html> <html> <div ng-app=”moduleName”> … </div> ... </html>
  • 26. Modules var app = angular.module(‘moduleName’, [] ) .config(function () { ... }).run(function () { ... }); app.controller(function(){ … }); app.service(function(){ … }); app.directive(function(){ … });
  • 27. Controllers Controllers are where we define our app’s behavior by defining functions and values. app.controller('ViewCtrl', ['$scope', '$location', 'recipe', function($scope, $location, recipe) { $scope.recipe = recipe; $scope.edit = function() { $location.path('/edit/' + recipe.id); }; }]); <html> <div ng-controller=·ViewCtrl”> … </div> ... </html>
  • 28. $scope Scope is an object that refers to the application model. VIEW CONTROLLER$scope <input type=”text” ng-model=”name” /> function SimpleController($scope) { $scope.name = “Leo”; }
  • 29. Services Services are singletons objects that are instantiated only once per app. Services are used to organize and share code across your app. Controllers are view-specific, services are app- specific.
  • 30. Built-in Services ● $http ● $q ● $window ● $location ● ...
  • 31. Built-in Services - $http var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function() { if (xmlhttp.readystate == 4 && xmlhttp.status == 200) { var response = xmlhttp.responseText; } else if (xmlhttp.status == 400) { // Handle error gracefully } }; // Setup connection xmlhttp.open(“GET”, “http://myserver/api”, true); // Make the request xmlhttp.send(); $http.get('api/user', {params: {id: '5'} }).success(function(data, status, headers, config) { // Do something successful. }).error(function(data, status, headers, config) { // Handle the error });
  • 32. Forms Forms and controls provide validation services, so that the user can be notified of invalid input. This provides a better user experience. <div ng-form=”loginForm” > <input type=”text” ng-model=”user.name” name=”uName” required /> <button ng-click=”update(user)” ng-disabled=”loginForm.$invalid”> SAVE </button> </div>
  • 33. Debugging Batarang → Google Chrome Extension