Angular JS Part 1
Angular JS Part 1
2. Sublime Text
4. Eclipse
Developers)
6. Jetbrains Webstorm
Tools for Angular JS
Batarang stable
(search for : batarang chrome extension for
angular)
https://github.com/angular/angular-seed
View
HTML
The interface
How the data is presented to the user
Model - View Controller
MVC is design pattern implemented in Angular JS for
Separation of concern ,Reusability, Maintainability ,
Testability and Extensibility
One way Data Binding
Angular JS - Two way data binding
Expressions
{{ }}
Angular expressions
Like JavaScript expressions except:
Evaluated in the current scope
No control statements: conditionals,
loops
Insert model values directly into the view
<span>{{expression}}</span>
Expressions
Directives
Teach new tricks to HTML
ng is angular directive
Angular Directives
Make use of special ng attributes
(directives) on the HTML elements (ng
means Angular Directive)
ng-app
Determines which part of the page will use
AngularJS
If given a value it will load that application
module
ng-controller
Determines which Javascript Controller should
be used for that part of the page
ng-model
Determines what model the value of an input
Angular JS API Reference
data-ng-app=
data-ng-
model=
data-ng-view =
Scope
<body ng-init=name='AngularJS'">
<input type="text" ng-model="name"/>
<div><h2>Hello, {{name}}</div>
Modules
In AngularJS, applications are structured
inmodules
angular.module('firstModule',[]);
//defines a module with no dependencies
angular.module('firstModule',
['moduleA','moduleB']);
//defines a module with 2 dependencies
Controller
Dependency
Injection
Modules
Attaching controller to Module:
You can attach Factory, Service,s Directives and
Filters
var firstModule=angular.module('firstModule',
[]);
firstModule.controller
(MyController',function($scope){
// register a controller
. });
firstModule.directive('FirstDirective',function(){
// register a directive
return { ..
Flaw: It };
is added to JavaScript global
}); scopeand it's not good practice to
Modules
What controller should not do:
angular.module('firstModule',[])
.controller('FirstController ,function($scope){
})
.directive('FirstDirective',function(){
//your directive
return {
};
});
Modules strcuture
Modules strcuture
DEMO
Modules
Defined to use
routes
For data
access logic
(CRUD)
uses
factories /
services/
providers.
Application Flow in Angular JS
Application Flow in Angular JS
Application Flow
After the browser loads the HTML and parses
it into a DOM, the angular.js script file is
loaded.
$(document).ready(function(){
// do jQuery
})
Application Flow
In the Angular.js file, towards the end, after
the entire code has been parsed by the
browser, you will find the following code:
jqLite(document).ready(function() {
angularInit(document, bootstrap);
});
//index.html
<!doctype html>
<html ng-app="tempApp">
<head>
...
// app.js
..
angular.module('tempApp', ['serviceModule'])
..
Application Flow
In turn, the $injector service will create
$rootscope, the parent scope of all Angular
scopes
Directive Flavours:
E
M
Directives
ng-repeat: used to instantiate the template for
each item in the source collection received from
the scope of the controller.
.directive('helloWorld', function() {
return {
restrict: 'AEC',
replace: true,
template: '<h3>Hello, World!</h3>'
};
});
Custom Directives
use camelCase to name the directive, when
using it in the
HTML , separate the words with dashes (-) or
colons (:) (Snake case):
<hello-world/> or <div hello-world />
Or:
<hello:world/>
<div data-hello-world></div>
Or:
<div x-hello-world></div>
Configuration Options
restrict: This provides a way to specify how a
directive should be used in HTML
DEMO
Dependency Injection - DIP
Dependency Injection
The DI framework's in charge of creating
components, resolving their dependencies, and
passing them to other components when
requested.
function SimpleController($scope,$rootScope,
$http){
$scope.someModel="Hello, World!";
}
The controller declares its dependencies through
the method parameters.
app.config(function($provide) {
$provide.provider($GreetService', function() {
this.$get = function() {
return function(name) {
alert("Hello, " + name); }; };
}); });
Dependency Injection
In previous code, new provider for a service
calledgreeting inject a variable named
$GreetingService into any injectable function
and Angular will call the
provider's$getfunction in order to return a
new instance of the service.
app.controller('MainController',
function($scope, $GreetService) {
$scope.onClick = function()
{ $GreetService(Murthy'); };});
Dependency Injection
Factory,service, andvalueare all just
shortcuts to define a provider.
app.config(function($provide) {
$provide.factory('alertService',function(){
return function(){
alert('Service in Action');
}
});
});
$injector.instantiate(TestController);
angular.module('myApp')
.service('customService',function($injector){
var $http=$injector.get('$http'); //get $http
service
var $rootScope=$injector.get('$rootScope');
});
Animation - ng-animate
Ng-model Data Binding
The directivenganimate need to be declared
for theng-animateattribute to an element
that have another directive that changes the
DOM.
Style classes :
For Enter: .animate-enter
For Leave: .animate-leave
Ng-model Data Binding
CSS Transition
.animate-enter {
-webkit-transition: 1s linear all; /* Chrome */
transition: 1s linear all;
opacity: 0;
}
.animate-enter.animate-enter-active {
opacity: 1;
Animation site - Plunker
http://
www.nganimate.org/angularjs/ng-repeat/spi
nning-keyframes-animation
Samples :
Move
3D Cube
3D Rotate
Scale
Appear
Skew
Yo Yo
Spinning
Angular JS charts
http://codepen.io/brunoscopelliti/pen/zIstF
(capital i)
Above site contain code for creating charts with
angular
Best Practice
Best Practice File Organization
Best Practice File Naming
Best Practice Angular