(BOOK Notes) Ng-Book
(BOOK Notes) Ng-Book
code/first-app/angular-hello-world/src/index.html
<body>
<app-root></app-root>
</body>
</html>
The app-root tag is where our application will be rendered.
1.2. Component
1.2.1. Command: ng generate component name
Create single component
code/first-app/angular-hello-world/src/app/hello- Ts document component control
world/hello-world.component.ts
import { Component, OnInit } from '@angular/core'; Import dependencies from the module
@angular/core
Import{things} - destructing (ES6 and
Typescript)
OnInit: help initiate when run component
@Component({ Component decorators
selector: 'app-hello-world',
templateUrl: './hello-world.component.html', Metadata added to the code
styleUrls: ['./hello-world.component.css'] Decorating the class to be a component
})
Selector: define a new tag in makup.
Defint which DOM element this
component uses.
Template: load template form html to
index.html. Can add by specify url or just
add html elements
Style: style only apply to the component
export class HelloWorldComponent implements Declare and set property type
OnInit {
Name: string ; // declare property of class Constructor: function called when create
constructor() { new instance of this class
this.name = 'Felipe';
}
ngOnInit() {
Pass value to child component through @Input and [child component @input variable]
Render template in child-component.html
Accept inputs: configure in .ts
Pass input value
/app/user-list/user-list.component.html [parent] Pass from parent to child through @input
<li *ngFor="let single-name of names"> Render child-component in parent-
<app-user-item [name]="single-name"></app-user- template. Call components list
item>
</li> [name]=”single-name”: each app-user-
item component name property accept
value from single-name property of
parent
/app/user-item/user-item.component.ts [child] @input decorator: allow passing value
import {Input} from ‘@angular/core’; from a parent component
export class UserItemComponent implements OnInit {
@Input() name: string; }
button (click)="addArticle(newtitle,
newlink)"
Pass value by reference
Parent.html
<div>
<child-component
[child-variable]=”parent-element-name”></child-component>
</div>
Child.ts
Import input from @angular/core
Export class child component {
@Input child-variable: type; // declare input variable to accept value
}
Pass value within html view Add resolve within the
<input name=”title” #titleresolve>title</input> same view
<button (click)=”addvote(titleresolve)”>voteup</button>
1.6. Deployment
1.6.1. Deploying our app is the act of pushing our code to a server, where it can be
accessed by others.
1.6.2. • compile all of our TypeScript code into JavaScript (which the browser can
read)
1.6.3. • bundle all of our JavaScript code files into one or two files
1.6.4. • and then upload our JavaScript, HTML, CSS, and images to a server
1.6.5. Ultimately, this Angular app is an HTML file that loads JavaScript code.
2. Typescript
2.1. There are five big improvements that TypeScript bring over ES5:
2.1.1. • types
2.1.1.1. Advantage:
2.1.1.2. Prevent bugs at compile time during writing
2.1.1.3. Clarification of intention during reading.
2.1.2. • classes
2.1.3. • decorators
2.1.4. • imports
2.1.5. • language utilities (e.g. destructuring)
Declaring type
var fullName: string;
function greetText(name: string): string {
return "Hello " + name; }
2.2.2. Any
any is the default type if we omit typing for a given variable. Having a variable of
type any allows it to receive any kind of value:
2.2.3. Void
Using void means there’s no type expected. This is usually in functions with no
return value:
2.3. Classes
2.3.1. Properties
2.3.2. Methods: functions called in context of object. Need first initiate an instance of
class first
2.3.3. Constructor: a special method that is executed when a new instance of the
class is being created.
2.3.3.1. Constructor methods must be named constructor.
2.3.3.2. Can take in parameters but not return any value
2.3.4. Inheritance
Child class extends
super class existing
behaviour
Super(child-class
parameters)
Construct super class
based on given
parameters
Super.run()
Call super class
methods
Super(parameters):
construct an
instance of super-
class object
2.4. Utilities
2.4.1. Fat arrow function syntax
2.4.1.1. Function (input){actions;} can be shortened to input => action
2.4.1.2. => syntax shares the same this. As the surrounding code.
This.name refers to “nate”
as => syntax use same this
reference as the
surrounding
3.2.5. Output definition: (name of output listened )=”function called when something
new is sent to the output($event is a special variable represents the thing sent
to the output)”
1. Specify outputs in the @Component configuration
2. Attach an EventEmitter to the output property
3. Emit an event from the EventEmitter, at the right time
3.2.5.1. EventEmitter:
3.2.5.1.1. Maintain a list of subscribers
3.2.5.1.2. Publish events to them
1. Specify output
and type of
output[declaration]
2. Create instance
when component is
called [construction]
3. Emit output
method [action]
(single-component
output/ child
component
property)= “outside
property or method
from club
component/ parent
component
($output event from
child component)”
$event is passed to
ringWasPlaced as
message
[style.<cssproperty>]="value"
<div [style.background-color]=" 'yellow' ">
<div [style.background-color]="color"
style="color: white;">
{{ color }} background
</div>
Style vs [style]
NgClass <div [ngClass]="{bordered: false}"> static class
<div [ngClass]="{bordered: isBordered}"> dynamic class by object literal
<div [ngClass]="classesObj"> dynamic class by object that represents {bordered:
true/false}
NgFor Repeat a given DOM element(or a collection) and pass an element of the
array
*ngFor="let item of items; let variable-name = index"
• The let item syntax specifies a (template) variable that’s receiving each
element of the items array;
• The items is the collection of items from your controller.
Component.ts class
Let ..= index: index is for the collection
/forms';
4.2.2. • Validators give us the ability to validate inputs, any way we’d like
4.2.2.1. Assign validator to the FormControl object
4.2.2.2. Check status of the validator in the view and take action
1. Create instance
variable of type
abstractcontrol
2. Assign value to
the instance from
formControls input
3. FormControl/
AbstractControl
syntax:
‘formControl-
name’: [‘value’,
state]
Property ‘sku’ is of
type
AbstractControl
and can be
validated by
method
Use
form.controls[‘sku’]
to call sku without
creating sku
instance
4.2.3. • Observers let us watch our form for changes and respond accordingly
To watch for changes on a control :
1. get access to the EventEmitter by calling control.valueChanges. then
2. add an observer using the .subscribe method
4.2.4. NgModel
4.2.4.1. Binds a model to a form. Implements two-way data-binding
[(ngModel)]
[] for input, () for output
Indicates two-way
binding
4.2.4.2.