Angular Js
Angular Js
What is AngularJS
à Javascript Framework
à MVC
à for Rich Web Application
Development
à by Google
Why AngularJS
“Other frameworks deal with HTML’s shortcomings by
either abstracting away HTML, CSS, and/or JavaScript
or by providing an imperative way for manipulating the
DOM. Neither of these address the root problem that
HTML was not designed for dynamic views”.
$(function() {
$('#theSelect').on('change',
function() {
var value = $(this).val();
$('#theValue').text(value);
}
});
Some Features
<html ng-app>
<head>
<script src='angular.min.js'></script>
</head>
<body>
<input ng-model='user.name'>
<div ng-show='user.name'>Hi
{{user.name}}</div>
</body>
</html>
MVC
Model (Data)
View (UI)
Change Notifies
Event Notification
Changes
Controller (Logic)
MVC
Model JS Objects
View DOM
Controller JS Classes
Bootstrapping
Expressions
<div>
<p>My first expression: {{ 5 + 5 }}</p>
</div>
https://docs.angularjs.org/api/ng/directive
Directives
<ul>
<li ng-repeat=”x in names">
{{ x.name + ', ' + x.country }}
</li>
</ul>
</div>
Controllers
AngularJS controllers control the data of AngularJS
applications.
<div ng-app="myApp" ng-controller="personCtrl">
</div>
<script>
var app = angular.module('myApp', []);
app.controller('personCtrl', function($scope) {
$scope.firstName = "John";
$scope.lastName = "Doe";
$scope.fullName = function() {
return $scope.firstName + " " + $scope.lastName;
}
});
</script>
Filters
Filter is used to do some transformation of the scoped
Data. Filter is applied using the symbol | (pipe).
Filter Description
currency Format a number to a currency format.
filter Select a subset of items from an array.
lowercase Format a string to lower case.
orderBy Orders an array by an expression.
uppercase Format a string to upper case.
<div ng-app="myApp" ng-controller="costCtrl">
</div>
Server Interaction
<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
$http.get("http://www.w3schools.com/angular/customers.php")
.success(function(response) {$scope.names = response.records;});
});
</script>
Modules
<script>
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
$scope.firstName = "John";
$scope.lastName = "Doe";
});
</script>
</body>
$Scope
“Scope is an object that refers to the application model. It is
an execution context for expressions. Scopes are arranged
in hierarchical structure which mimic the DOM structure of
the application. Scopes can watch expressions and
propagate events.” - From Angular website
<ul class="phones">
<li ng-repeat="phone in phones | filter:query | orderBy:orderProp"
class="thumbnail">
<a href="#/phones/{{phone.id}}" class="thumb"><img ng-
src="{{phone.imageUrl}}"></a>
<a href="#/phones/{{phone.id}}">{{phone.name}}</a>
<p>{{phone.snippet}}</p>
</li>
</ul>
….
http://fastandfluid.com/publicdownloads/
AngularJSIn60MinutesIsh_DanWahlin_May2013.pdf
Single Page application