Tuesday, September 20, 2022

The complete book on angular 11 - Chapter 5 Built-in Directives

 The complete book on angular 11 - Chapter 5 Built-in Directives

1.

A FormControl represents a single input field - it is the smallest unit of an Angular

form.

FormControls encapsulate the field’s value, and states such as being valid, dirty

(changed), or has errors.

2.

the FormsModule gives us template driven directives such as:

• ngModel and

• NgForm

Whereas ReactiveFormsModule gives us reactive driven directives like

formControl and

• ngFormGroup

3.

<form #f="ngForm" (ngSubmit)="onSubmit(f.value)">

The #v="thing" syntax says that we want to create a

local variable for this view.

4.

Validaor

<div class="field"

[class.error]="!sku.valid && sku.touched">

 <label for="skuInput">SKU</label>

 <input type="text"

 id="skuInput"

 placeholder="SKU"

 [formControl]="sku">

 <div *ngIf="!sku.valid"

 class="ui error message">SKU is invalid</div>

 <div *ngIf="sku.hasError('required')"

 class="ui error message">SKU is required</div>

 </div>

Thursday, September 15, 2022

The complete book on angular 11 - Chapter 4 Built-in Directives

 The complete book on angular 11 - Chapter 4 Built-in Directives

1. ngSwitch

<div class="container" [ngSwitch]="myVar">

<div *ngSwitchCase="'A'">Var is A</div>

<div *ngSwitchCase="'B'">Var is B</div>

<div *ngSwitchDefault>Var is something else</div>

</div>

2. NgStyle

<div [style.background-color]="'yellow'">

Uses fixed yellow background

</div>

<div [ngStyle]="{color: 'white', 'background-color': 'blue'}">

Uses fixed white text on blue background

</div>

3. ngClass

<div [ngClass]="{bordered: false}">This is never bordered</div>

4. ngNonBindable

<div class='ngNonBindableDemo'>

<span class="bordered">{{ content }}</span>

<span class="pre" ngNonBindable>

&larr; This is what {{ content }} rendered

</span>

</div>

TODO Forms in Angular

Wednesday, September 14, 2022

The complete book on angular 11 - Chapter 3 How Angular Works

 Chapter 3 How Angular Works

1.

We’ve provided the entire, completed application in the code download folder under

how-angular-works/inventory-app.

2.

The @Component is called a decorator.

The Component controller is defined by a class.

<h1>{{ product.name }}</h1> //Using the {{…}} syntax is called template binding.

What’s neat about template binding is that the code inside the brackets is an

expression. That means you can do things like this:

• {{ count + 1 }}

• {{ myFunction(myArguments) }}

3.

When we use products-list we’re using a key feature of Angular components:

inputs and outputs:

<products-list

[productList]="products" <!-- input -->

(onProductSelected)="productWasSelected($event)"> <!-- output -->

</products-list>

The [squareBrackets] pass inputs and the (parentheses) handle outputs.

Data flows in to your component via input bindings and events flow out of your

component through output bindings.

(onProductSelected)="productWasSelected($event)": We’re saying that we want to listen to the onProductSelected output from the

ProductsList component.

import {

Component,

EventEmitter,

Input,

Output

} from '@angular/core';

import { Product } from '../product.model';

/**

* @ProductsList: A component for rendering all ProductRows and

* storing the currently selected Product

*/

@Component({

selector: 'products-list',

templateUrl: './products-list.component.html'

})

export class ProductsListComponent {

/**

* @input productList - the Product[] passed to us

*/

@Input() productList: Product[];

/**

* @output onProductSelected - outputs the current

* Product whenever a new Product is selected

*/

@Output() onProductSelected: EventEmitter<Product>;

we can listen to an event by using the (output)="action" syntax.

4.

<my-component [shortName]="myName" [oldAge]="myAge"></my-component>

@Input("shortName") name: string;

@Input("oldAge") age: number;

5.

To create a custom output event we do three things

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

An EventEmitter is an object that helps you implement the Observer

Pattern. That is, it’s an object that will:

1. maintain a list of subscribers and

2. publish events to them.

Here’s a short and sweet example of how you can use EventEmitter

let ee = new EventEmitter(); 

ee.subscribe((name: string) => console.log(Hello ${name})); 

ee.emit(“Nate”);

// -> “Hello Nate”

6.

The line [class.selected]="isSelected(myProduct)" is a fun one: Angular allows

us to set classes conditionally on an element using this syntax. This syntax says

“add the CSS class selected if isSelected(myProduct) returns true.”

7.

@HostBinding('attr.class') cssClass = 'item';

we’re saying that we want to attach the CSS class item to the host element.

TODO Built-in Directives

The complete book on angular 11 - Chapter 2 TypeScript

 Chapter 2 TypeScript

1. 

npm install -g typescript

npm install -g tsun (TypeScript Upgraded Node)

tsun

> var fullName: string = 'Nate Murray'

> console.log(fullName)

var jobs: Array<string> = ['IBM', 'Microsoft', 'Google']; equals with the below

var jobs: string[] = ['Apple', 'Dell', 'HP'];

enum Role {Employee, Manager, Admin};

var role: Role = Role.Employee;

console.log('Roles: ', Role[0], ',', Role[1], 'and', Role[2]);

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.

var something: any = 'as string';

something = 1;

something = [1, 2, 3];

2. 

//ES5-like example

var data = ['Alice Green', 'Paul Pfifer', 'Louis Blakenship'];

data.forEach(function(line) { console.log(line); });

However with the fat arrow => syntax we can instead rewrite it like so:

// Typescript example

var data: string[] = ['Alice Green', 'Paul Pfifer', 'Louis Blakenship'];

data.forEach( (line) => console.log(line) );

Parentheses are optional when there’s only one parameter.

data.forEach( line => console.log(line) );

3.

var firstName = "Nate";

var lastName = "Murray";

// interpolate a string (enclose your string in backticks)

var greeting = `Hello ${firstName} ${lastName}`;

console.log(greeting);

TODO: page 121

Sunday, September 11, 2022

The complete book on angular 11 - Chapter 1 Writing Your First Angular Web Application

Chapter 1: Writing Your First Angular Web Application


1. To get help, go to community chat room:

https://newline.co/discord/ng-book

https://github.com/srdeleon/angular-reddit ?

2.

To get started with Angular, you’ll need to have Node.js installed.

The Node Package Manager (npm for short) is installed as a part of Node.js.

To check, run

npm -v

Once you have Node.js setup, the next step is to install TypeScript.

npm install -g typescript

tsc -v

Install Angular CLI:

npm install -g @angular/cli

Once it’s installed you’ll be able to run it from the command line using the ng

command.

ng help

3.

ng new angular-hello-world

ng serve/ng serve --port 9001

ng will look at the file angular.json to find the entry point to our app.

angular.json specifies a "main" file, which in this case is main.ts

main.ts is the entry-point for our app and it bootstraps our application

We use the AppModule to bootstrap the app. AppModule is specified in src/app/app.module.ts

AppModule specifies which component to use as the top-level component. In this case it is AppComponent

4.

ng generate component hello-world

Using backticks `` for multiline strings makes it easy to put templates inside your code files.

5.

<ul>

    <li *ngFor="let name of names">Hello {{ name }}</li>

</ul>

6.

@Input() name: string | undefined;


<ul>

    <li *ngFor="let individualUserName of names">

        <app-user-item [name]="individualUserName"></app-user-item>

    </li>

</ul>

7.

ng new angular-reddit

Notice that in the input tags we used the # (hash) to tell Angular to assign those tags

to a local variable. By adding the #newtitle and #newlink to the appropriate <input/> elements, we can pass 

them as variables into the addArticle() function on the button!

console.log(`Adding article title: ${title.value} and link: ${link.value}`);

8.

@HostBinding('attr.class') cssClass = 'row';

9.

JavaScript, by default, propagates the click event to all the parent components.

Because the click event is propagated to parents, our browser is trying to follow the

empty link, which tells the browser to reload.

To fix that, we need to make the click event handler to return false

10.

The votes parameter is optional (denoted by

the ? at the end of the name) and defaults to zero.

    constructor(title: string,link: string, votes?: number){

        this.title = title;

        this.link = link;

        this.votes = votes || 0;

    }

11.

ng build --prod -> deprecated and removed

ng build --configuration production

Thursday, September 8, 2022

RxJS and NgRx

1. RxJS

2. NgRx

Each reducer function is a listener of actions.