»
export class Navigation {
constructor (private navService:tavService) {}
https:/iriptutorial. com/ 13selectedNaviten (item: number) {
console.log("selected nav item ' + item);
this. navService.enitNavChangeEvent (item) ;
Read Event Emitter online: https://riptutorial com/angular/topic/9828/event-emitter
https://riptutorial.com/
14Chapter 3: For Loop
Examples
NgFor - Markup For Loop
The NgFor directive instantiates a template once per item from an iterable. The context for each
instantiated template inherits from the outer context with the given loop variable set to the current
item from the iterable.
To customize the default tracking algorithm, NgFor supports trackBy option. trackBy takes a
function which has two arguments: index and item. If trackBy is given, Angular tracks changes by
the return value of the function.
{(1h) = ({item.name})
Additional Options: NgFor provides several exported values that can be aliased to local
variables:
+ index will be set to the current loop iteration for each template context.
first will be set to a boolean value indicating whether the item is the first one in the iteration.
last will be set to a boolean value indicating whether the item is the last one in the iteration.
even will be set to a boolean value indicating whether this item has an even index.
‘odd will be set to a boolean value indicating whether this item has an odd index.
Read For Loop online: https://riptutorial.com/angular/topic/9826/for-loop
https:/iriptutorial. com/ 15Chapter 4: Forms
Examples
Reactive Forms
app.module.ts
Add these into your app.module.ts file to use reactive forms
import { NgModule } rrom ‘eanguiar/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule, ReactiveFormsModule } from '@angular/forns';
import { AppComponent } rrom *./app.component';
@igModule (
imports: |
Erowserliodule,
FormsModule,
ReactiveFormsModule,
7
declarations: [ AppConponent ]
providers: (J,
bootstrap: [ AppConponent ]
»
export class AppModule (}
app.component.ts
import { Component, OnInit } from '@angular/core';
import template from './app.component -html';
import ( FornGroup,FornBuilder, Validators } from '@angular/forns';
import { matchingPesswords } fron './validators';
@component ({
selector: ‘app',
template
»
export class AppComponent implements Onini: (
addForm: FormGroup:
constructor (private formBuilder: FormBuilder) (
)
ngOninit() |
this.addForm = this. formauilder.group|(
beernane: ['', Validators. required),
email: ['', Validators. required],
role: ["", Validators.requiredl,
password: [1", Validators. required),
password2: ['', Validators.required]
} ( validato:
matchingPasswords (*password', ‘password2") });
https:/iriptutorial. com/
16ui
addUser () {
if (this.addForm.valid) {
var adduser = (
usernane: this.addForm.controls|‘usecnane'].value,
email: this.addForm.controls['email'] value,
password: this addForm.controls|'password') value,
profile: |
role: this.addForm.controls['role'] .value,
name? thie.addForm.controis['usernane"}.value,
email: this-addForm.controls["email'] .value
Ms
console. log (adduser);// adduser var contains all our form values.
ven enat
this.addForm.reset ();// this will reset our form values to null
app.component.html
https:/iriptutorial. com/[(ngModeL)
'signUpRequest.email® />
({ status })
»
export class AppComponent ‘7
6. Link the other routes. By default, scuteroutet will load the component for which empty path
is specified in the soutes. rovtersink directive is used with html anchor tag to load the
components attached to routes. soute:tinc generates the href attribute which is used to
generate links. For example:
Amport { Component ) from ‘@angular/core';
@component ({
selector: ‘demo-app',
template:
LoginsignupDashboard
https:/iriptutorial. com/ 26
»
export class AnotherComponent { }
Now, we are good with routing to static paths. souteriink supports dynamic path too by passing
extra parameters along with the path.
import ( Component } from *@angular/core';
@Component ({
selector: ‘deno-app',
template:
(jbar.nane}
let bar of bars | agynet>
»
export class SecondComponent { }
sousertink takes an array where the first parameter is the path for routing and subsequent
elements are for the dynamic routing parameters.
Read Routing online: https://riptutorial.com/angular/topic/9827/routing
https:/iriptutorial. com/
27Chapter 7: RXJS and Observables
Examples
Wait for multiple requests
One common scenario is to wait for a number of requests to finish before continuing. This can be
accomplished using the... method.
In the following example, sorkcoin is used to call two methods that retum onsexvabies. The callback
specified in the .subscrsbe method will be called when both Observables complete. The
parameters supplied by . suscrine match the order given in the call to . sorkuoin. In this case, first
posts then tags.
loadData() + void {
Observable. forkJoin(
this.blogApi.getPosts(),
this.blogapi..getTags ()
).subscribe((([posts, tags]: (Post {], Tagl]]) => (
this.posts = posts;
this.tags = tags;
me
Basic Request
The following example demonstrates a simple HTTP GET request. ncep.ce: () retums an
obseevabie Which has the method sunscsive. This one appends the returned data to the poses array.
var posts = [1
gerPosts (http: Http): (
this-http.get (“https://jsonplaceholder. typicade.com/posts*|
-subscribe (response => {
posts.push(response. json());
ne
Read RXJS and Observables online: https://riptutorial.com/angular/topic/9829/rxjs-and-
observables
https:/iriptutorial. com/ 28Chapter 8: Sharing data among components
Introduction
The objective of this topic is to create simple examples of several ways data can be shared
between components via data binding and shared service.
Remarks
There are always many of ways of accomplishing one task in programming. Please feel free to edit
current examples or add some of your own.
Examples
Sending data from parent component to child via shared service
service.ts:
import { Injectable } fron '@angular/core';
@Injec
eo
38 AppState (
export c!
oF
public mylis
parent.componentts:
import {Component} fron 'Cancular/core';
import { AppState } from './shared.service';
@component ((
selector: ‘parent-example',
cemplateUrl: ‘parent.conponent.ntnl',
»
export class ParentComponent {
mylistFromarent = {17
constructor (private appState: Appstate) {
this.appstate.mylist;
)
aay) {
this-appState.mylist .push({"itenNare":"Sonethina"}) ;
}
parent.component.html:
https:/iriptutorial. com/
29