Pankaj Parkar
Email: parkarpankaj3@gmail.com
Twitter: @pankajparkar
FB: fb.com/pankaj.parkar
LinkedIn: https://in.linkedin.com/in/pankajparkar
Data binding & code mentainability
<p align="right">
Content
</p>
document.querySelector('p')
what to do, not how to do it
var numbers = [1,2,3,4,5]
var total = 0
for(var i = 0; i < numbers.length; i++) {
total += numbers[i]
}
console.log(total) //=> 15
Calculate Sum
Get odd number collection
var results = collection.Where(
num => num % 2 != 0
);
What you want.
Get odd number collection
Calculate Sum
var results = collection.Sum(
x => x
)
<label>Username</label>
<input type="text" id="username" class="username" />
<label>Password</label>
<input type="password" id="password" class="password" />
<button onclick="submit()"> Login </button>
$("#username").on('keyup', function(event){
var value = event.target.value;
//Some awesome logic
if(valid)
//remove disable attribute from button
else
//add disable attribute for button
});
$('#password').on('keyup', function(event){
var value = event.target.value;
//Some awesome logic
if(valid)
//remove disable attribute from button
else
//add disable attribute for button
});
$('#form').submit(submit);
function submit(){
//check form validity
//submit form date to server if valid
};
This code is imperative code which goes on increasing as number of field will be increased
Login Form
<form name="loginForm" ng-submit="submit(model)">
<label>Username</label>
<input type="text" ng-pattern="/^[a-zA-Z]$/" class="username" ng-model="model.userName"/>
<label>Password</label>
<input type="password" id="password" class="password" ng-model="model.password"/>
<button ng-disabled="loginForm.invalid"> Login </button>
</form>
We used declarative approach to made form working.
Framework supported on all platform
Superfast
Incredible tooling
Great community support.
Dependency Injection
Two way data bindings
Forms
Server side rendering
AOT (Ahead Of Time Compilation)
Modular & Testable code
5x faster than AngularJS
Component Architecture
Single Framework for everything
It is basic building block of an Angular application
It meant to server single purpose
Gmail / Google example
@Component({
selector: 'greet',
template: 'Hello {{name}}!',
//templateUrl: '/path/to/template.html'
styles: ['.app { display: block }'],
providers: [],
styleUrls: ['/app/path/to.css']
})
export class GreetComopnent {
name: string = 'World';
}
Inject it inside NgModule `declarations` metadata option
Main component should be injected inside bootstrap of your application
Component class should be exported to use it in module.
Used to modularize Angular application
It help organize an application into cohesive blocks of functionality.
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
@NgModule({
imports: [ BrowserModule ], //other modules
declarations: [ AppComponent ], //components, directives, pipes
bootstrap: [ AppComponent ] //bootstrap component
})
export class AppModule { }
Class should be exported
All metadata options should be filled correctly
AppModule
Service
Comp1
Comp3
Pipe
Sub Module
In general router responsible for perform action based on url pattern provided it.
<body>
my-html-content
....
<some-placeholder></some-placeholder>
..Other html
</body>
{
'pattern': '/my-path', action: 'doSomething',
'pattern': '/my-path-2', action: 'doSomethingElse',
...
}
Config
Two ways to write configure router
Eager loading
Lazy loading
Make sure, you have loaded RouterModule inside your application
Register routes before importing module
Data Bindings
{{myData}}
Property Binding
[myData]="test"
Event Binding
(eventName)="methodCall($event)"
Pizza order example
Dependent code execution which should to fetch response from API before proceed to next function.
function errorFn(error){ console.log(error) }
task1(function successFn(data){
console.log(data);
task2(function successFn(){
task3(function successFn(){
task4(function successFn(data){
console.log(data);
task5(function successFn(){
task6(function successFn(){
task7(function successFn(data){
console.log(data);
task8(function successFn(){
task9(function successFn(){
task10(function successFn(data){
console.log(data);
console.log("We're done");
}, errorFn)
}, errorFn)
}, errorFn)
}, errorFn)
}, errorFn)
}, errorFn)
}, errorFn)
}, errorFn)
}, errorFn)
}, errorFn)
function errorFn(error){
console.log(error)
}
function successFn(data){
console.log(data);
return data;
}
task1(task2Fn,errorFn)
.then(task3Fn, errorFn)
.then(task4Fn, errorFn)
.then(task5Fn, errorFn)
.then(task6Fn, errorFn)
.then(task7Fn, errorFn)
.then(task8Fn, errorFn)
.then(task9Fn, errorFn)
.then(task10Fn, errorFn)
var items = [1, 2 , 3, 4];
//add them
var result = 0;
items.forEach(function(item){
result += item;
});
console.log('Total', result);
Observable
Listner
Listner
Stream
Stream
Observable(array)
Listner
Listner
Stream
Stream
[1, 2, 3, 4]
push 5, 6
@Injectable() //< --this is important part
export class GreetService {
name: string = 'World';
sendSomeMessage(){
console.log("Hello world ", this.name)
}
}
1
2
2
3
last
routerLink directive
router.navigate method
Forms API made form validation super easy in Angular
Two Types of Form
Template Driven
Model driven
When to use what?
3rd party library without typescript implementation(Toastr)
Some other third party library
Angular Good Blogs
http://thoughtram.io/
johnpapa.net
toddmotto.com
https://teropa.info
angular.io
https://netbasal.com/@NetanelBasal
https://medium.com/@maximus.koretskyi
Docs
https://angular.io/docs
https://angular.io/api (For API import check)
Best Practices
Coding guidelines by John Papa https://angular.io/guide/styleguide
Books
https://angular-2-training-book.rangle.io/
https://codecraft.tv/courses/angular/
https://github.com/pankajparkar/bug-tracker
(do click star button there, if you like it)