Angular12guide
Angular12guide
Angular12guide
https://angular.io/start
Angular is a development platform, built on TypeScript. Angular includes:
Components
@Component({
selector: 'hello-world',
template: `
<h2>Hello World</h2>
<p>This is my first component!</p>
})
<hello-world></hello-world>
When Angular renders this component, the resulting DOM looks like this:
<hello-world>
<h2>Hello World</h2>
</hello-world>
Components also make your application painless to unit test and can
improve the overall readability of your code.
Templates
Every component has an HTML template that declares how that component
renders. You define this template either inline or by file path.
Angular extends HTML with additional syntax that lets you insert dynamic
values from your component.
selector: 'hello-world-interpolation',
templateUrl: './hello-world-interpolation.component.html'
})
When the application loads the component and its template, the user sees
the following:
<p>Hello, World!</p>
Angular also supports property bindings, to help you set values for properties
and attributes of HTML elements and pass values to your application's
presentation logic.
</p>
Notice the use of the square brackets--that syntax indicates that you're
binding the property or attribute to a value in the component class.
Declare event listeners to listen for and respond to user actions such as
keystrokes, mouse movements, clicks, and touches. You declare an event
listener by specifying the event name in parentheses:
<button
type="button"
[disabled]="canClick"
(click)="sayMessage()">
sayMessage() {
alert(this.message);
hello-world-bindings.component.ts
hello-world-bindings.component.html
hello-world-ngif.component.ts
hello-world-ngif.component.html
Templates are based on standard HTML, for ease in building, maintaining, and
updating.
Dependency injection
Dependency injection lets you declare the dependencies of your TypeScript
classes without taking care of their instantiation.
This design pattern lets you write more testable and flexible code.
The first file, logger.service.ts, defines a Logger class. This class contains
a writeCount function that logs a number to the console.
@Injectable({providedIn: 'root'})
console.warn(count);
This component contains a button that uses the writeCount function of the
Logger class.
@Component({
selector: 'hello-world-di',
templateUrl: './hello-world-di.component.html'
})
count = 0;
onLogMe() {
this.logger.writeCount(this.count);
this.count++;
Angular CLI
You'll find the Angular CLI a valuable tool for building out your applications.
First-party libraries
Angular Robust HTTP client that can power more advanced client-
HttpClient server communication.
These libraries expand your application's functionality while also letting you
focus more on the features that make your application unique.
https://stackblitz.com/edit/angular-ivy-cwrbr4
Components define areas of responsibility in the UI that let you reuse sets of
UI functionality.
This section guides you through editing the HTML, also known as the
template.
<h2>Products</h2>
</div>
With *ngFor, the <div> repeats for each product in the list.
<h2>Products</h2>
<h3>
{{ product.name }}
</h3>
</div>
The preview pane updates to display the name of each product in the
list.
4. To make each product name a link to product details, add
the <a> element around {{ product.name }}.
<h2>Products</h2>
<h3>
{{ product.name }}
</a>
</h3>
</div>
In the preview pane, hover over a product name to see the bound
name property value, which is the product name plus the word
"details". Property binding [ ] lets you use the property value in a
template expression.
<h2>Products</h2>
<div *ngFor="let product of products">
<h3>
{{ product.name }}
</a>
</h3>
<p *ngIf="product.description">
Description: {{ product.description }}
</p>
</div>
The application now displays the name and description of each product
in the list. Notice that the final product does not have a description
paragraph. Angular doesn't create the <p> element because the
product's description property is empty.
<h2>Products</h2>
<div *ngFor="let product of products">
<h3>
{{ product.name }}
</a>
</h3>
<p *ngIf="product.description">
Description: {{ product.description }}
</p>
</div>
Clicking the Share button triggers an alert that states, "The product
has been shared!".
Pass data to a child component
Currently, the product list displays the name and description of each
product. The ProductListComponent also defines a products property that
contains imported data for each product from the products array
in products.ts.
The next step is to create a new alert feature that uses product data from
the ProductListComponent. The alert checks the product's price, and, if the
price is greater than $700, displays a Notify Me button that lets users sign up
for notifications when the product goes on sale.
1. Click on the plus sign above the current terminal to create a new
terminal to run the command to generate the component.
The generator creates starter files for the three parts of the
component:
o product-alerts.component.ts
o product-alerts.component.html
o product-alerts.component.css
3. Open product-alerts.component.ts.
@Component({
selector: 'app-product-alerts',
templateUrl: './product-alerts.component.html',
styleUrls: ['./product-alerts.component.css']
})
constructor() { }
ngOnInit() {
constructor() { }
ngOnInit() {
</p>
declarations: [
AppComponent,
TopBarComponent,
ProductListComponent,
ProductAlertsComponent,
],
Share
</button>
<app-product-alerts
[product]="product">
</app-product-alerts>
The new product alert component takes a product as input from the product
list. With that input, it shows or hides the Notify Me button, based on the
price of the product. The Phone XL price is over $700, so the Notify
Me button appears on that product.
Pass data to a parent component
To make the Notify Me button work, the child component needs to notify and
pass the data to the parent component. The ProductAlertsComponent needs
to emit an event when the user clicks Notify Me and
the ProductListComponent needs to respond to the event.
</p>
4. Define the behavior that happens when the user clicks the button. The
parent, ProductListComponent—not the ProductAlertsComponent—acts
when the child raises the event. In product-list.component.ts, define
an onNotify() method, similar to the share() method.
src/app/product-list/product-list.component.ts
products = products;
share() {
onNotify() {
Share
</button>
<app-product-alerts
[product]="product"
(notify)="onNotify()">
</app-product-alerts>
6. Click the Notify Me button to trigger an alert which reads, "You will be
notified when the product goes on sale".