Ujnotes — CC0 1.0
Working notes on Notes: Annotations, ng-, Variables, $scope, Iterate, and /.
Frontend.
Enhanced HTML for Web apps.
Model View -WhateverModel : JS.
View : HTML.
Style: SCSS.
Annotations
ng-
App.
Controller.
Variables
$scope
Mother variable.
Iterate
https://stackoverflow.com/questions/29953198/foreach-loop-in-angularjs
For(let e of elements).
Routing.
URLs.
/
From room.
/ <No slash>Relative.
Private variables won’t leak on to the html side - and can be accessed only in the ts files
e.g. constructor(private <service>: <instance>)There can be only a single form group on a page.
Execution path
\\ angular.json > build > main > <path/<main_file>.ts> > boostrapModule()main.ts creates browser environment for the application to run
Imports { platformBrowserDynamic } from '@angular/platform-browser-dynamic';.
Calls bootstrapModule(AppModule).
PlatformBrowserDynamic().bootstrapModule(AppModule).
Module, component and service
Component is a piece of UI
Defined using @Component decorator.
Has three parts.
1. .ts file
- Has class.
2. .html
- Template - with dom piece.
- can be presented inplace by enclosing in tildes.
3. .scss
- Stylesheet.
Selector.
TemplateUrl.
StyleUrls.
ng generate component <component_name>Ng g c test.
@Component({
@Component({ templateUrl: "./app.<component>.html",selector. "app",.
}).
export class AppComponent {
Data = "This is an example component of two way data binding.";.
}.
Modules
Modules to group components.
One whole module is transported as one unit.
Two types.
1. root
- Only one.
- imports BrowserModule.
2. Feature
- imports CommonModule.
Registered in module providers.
Service
Single instance class that facilitate communication across classes.
@Injectable decorator.
Provides methods that can be invoked directly by importing the service.
ng g s <service-name>Scope
Binder between controller(js) and view(html).
Object - available to both.
Data binding
Direct communication between DOM and component - no explicit data push pull.
1. Property binding
1. DOM element’s property : field in Component
- Handled internally.
2. Event binding
1. DOM event. Component method.
3. String binding
1. DOM element. {{ Component_field }}.
4. Two way data binding
1. DOM element filed. Component field.
- One end immediately reflects on another end.
5. <input [(ngModel)]="data" type="text">6. <h2>You entered the data: {{data}}</h2>Decorators
Modify service, directive or filter.
Provides configuration metadata
Provides configuration metadata @<decorator>({<metadata_property>: ‘<value>’...Provides configuration metadata export class <class> {)}.
}.
- Method.
2. Class
1. specify. Component or module.
3. Parameter
- arguments to class constructors.
2. add functionality
- e.g. @HostListener(‘click’, [‘$event’]).
4. Property
1. @Input(), @Output, @ReadOnly(), @Override()
1. Output. From child component to parent.
- class property.
Annotations
Hard-coded. Metadata - set on class to ‘reflect’ metadata library.
Annotations property added to class - array - annotation saved.
Instance name same as annotation.
Pipes
Used to transform data.
@Pipe({
@Pipe({ name: ‘<pipe_name>’;pure. [false|true].
}).
export class <Pipe_name> {}Progression from left to right.
1. Pure
- only methods - no properties - stateless.
2. Impure
- impure method - has propertie(s) and thus state.
- Runs on every change detect cycle.
- Heavy on cpu resource.
3. Pipe transform Interface
- for custom transform.
2. 1st param - value of binding
- 2nd param - list of arguments.
4. Pipe chaining
4. Pipe chaining 1. <var>|<pipe_1_transform>|<pipe_2_transform>Share data
@input - @output.
export class <component> {@Input() <property>: <type>;}.
Parent to child
Use property directly.
Child to parent
Child to parent @ViewChild(<ChildComponent>, {static:[true|false]} <child>;ngAfterViewInit() {
ngAfterViewInit() { this.<dataFromChild> = this.<child>.<data>}.
Banana in Box
[()].
For bi-directional sharing of data.
Loading.
1. Eager
- pre-load before execution begins.
2. Lazy
- Load modules as needed.
View encapsulation
Specifies if specific component’s template and style can impact the entire app or vice versa.
1. Native
- No inheritance.
- Limited to component.
2. Emulated
- Inheritance.
- Limited to component.
3. None
- Inheritance?
- Not limited to component.
- “styles will be repeated in each component - with native encapsulation?
RxJS
Reactive extensions for JavaScript.
Observables -> Reactive programming.
Async / callback - based code -> sequence of operations on data streams
Publisher to subscriber.
Often used with http requests - as it is async.
Subscribe.
Data binding
Interconnection between model and view.
String interpolation
{{ }}
Content is executed.
For example, 1+1 => 2.
Part of data binding.
One way.
Property binding
[]
Property of DOM element. Component’s property.
Part of data binding.
One way
Component logic to view.
Event binding
()
target listens to events. Click, keypress.
Two way
[()]
Banana in box.
Observables v/s promise.
1. Observables
- can be both async or synchronous.
- Can emit multiple values - i.e. multiple times.
- Lazy - called only when subscribed to.
- Unsubscribe() to cancel.
- Operators, multiple - map, forEach, filter, reduce, retry, retryWhen….
2. Promise
- always async.
- can only emit single value - i.e. once.
- Not lazy.
- Cannot be canceled.
- No such operators.
Dependency injection
Design pattern.
Services.
Component
onInit
onInit Constructor(<service>) {}Input
pattern
[]{}.
For example, .
Lifecycle
Applicable to components.
Initialized - then - destroyed.
Hooks - tap into the lifecycle events.
- Constructor.
- ngOnChanges.
3. ngOnInit
- called after first ngOnChanges.
- If lot of processing - better here instead of constructor.
- Initializes component and sets input properties.
- no arguments, and returns void.
4. ngDoCheck
- after ngOnInit.
- used to detect changes that cannot be detected by Angular.
- custom change detection algorithm.
5. ngAfterContentInit
- after ngDoCheck.
- after ?content gets projected? into component.
6. ngAfterContentChecked
- after ngAfterContentInit and after every subsequent ngDoCheck.
- responds after ?projected content is checked?
7. ngAfterViewInit
- after view initialized.
8. ngAfterViewChecked
- after ngAfterViewInit.
- after view checked.
9. ngOnDestroy
- after component destroyed by Angular.
- to be used to clean up and detach event handlers.
Imports OnInit but implements ngOnInit.
Same with other hooks.
Router links
Router links <a routerLink=’/<path>’><title></a>Router links <router-outlet></router-outlet>Router state
Route tree.
nodes. ‘consumed’ url segments, retrieved arguments and ?processed data?.
Uses router service and router state property to get current RouterState from anywhere.
Angular Material
User interface component.
uniform - consistent, appealing, functional - contemporary design concepts: gentle degradation and browser portabilityTranspiling
Compiling one language code into another.
e.g. TS is transpiled into JS code by Angular
Dart can also be used. AngularDart.
Internal.
Http intercept
Both HttpRequest & HttpResponse can be intercepted.
For example, to add auth headers at one place.
AOT
Ahead of time compilation.
Compiled during build time - i.e. pre production.
Faster rendering.
Fewer ajax loads.
Error detection during build phase.
Fewer files - better security.
Default is JIT.
for AOT. Ng build –aot, ng serve –aot.
Change detection
Process of synchronizing view with model.
One directional, from root to children.
?all components are children, themselves Not parent?
Bootstrapping module
Initial.
Popular. ‘AppModule’.
MVVM
Model, View, ViewModel.
Model
Data model.
View
Visual layer.
ViewModel
Abstract layer - manages business logic and translation.
Bound to view - data-binding - two way.

