Angular 7 Guide
Angular 7 Guide
ngOnInit
eg2: element.nativeElement.style.color="red";
3. HostListener
Eg1: @HostListener('eventType') fucntionName(){
Do any thing....
}
elemClicked(elem)
{
console.log(elem);
}
This eg 2 is for callig a eleClicked function where ever the clicked inside
document.
4. ViewChild
@ViewChild
Creating a reference to a tag.. Like getting the value(all attribute) from the
custom name
@ViewChild('myInputText') inputText;
7. Advance ngFor
HTML:
Typescript:
let result=2;
The diff between both inut tag is {{}}--> this evaluate and convert to string
if []--> this will convert to string
routerLink
Using of href will reload the page but routerLink wont
<a routerLink="path"></a> here path is url
<a [roterLink]="custompath"></a> and here custom path is varaible
routerLinkActive
Create Routes
11. Service
(To store value/pass value of a value across component)
Its a singale instance where it can be used to all the component under decalrtion
in module section.
it will generete/add guard file into application and it will automatically add
canActivate method (which is fired before roter takes place. if it return false
router stops)
To use it add it in the route config of ur module and import it abd also add it in
provider
const appRouters: Routers =[
{
path:'login',
canActivate : [AuthenticateGaurd],
component: LoginComponent
}
Go to component.ts
import {ActivatedRoute} from '@angular/router';
localhost/user/nissan
//inside component.ts
Eg: name='';
this.name=this.route.snapshot.params.name
and it html
If pathMatch : 'full'
15. @Input
import EventEmitter
eg: @OutPut() myOutput =new EventEmitter(); // will emit value to parent
this.myOutput.emit(variableName/value);
Parent Compoent :
//TS File
newfucntion(e)
{
console.log(e,'some text'); // to listen, add it where the compoent tag is called
}
//HTML File
Eg:
<app-child (myOutput)="newFucntion($event)"> // we are listing gr event
a. Template Driven
b. Reactive Forms
now in that argument we will be able to get the complete form data from the
argument newForm
userList:user[]=[];
Validation:
To diable the submit button if the form in not valid
b. Pattern Attribute
eg: pattern="add regex for the input field" //add it in html
eG: maxlength =10 //add it in html tag of the input field. not more that 10 digit
is allowed
c. Disaplay error message in input field
#nameUser="ngModel"
//where nameUser is reference varaible and add it ngModel to get control
//To display error message only after enter/ focus comes and go out/ tocuhed that
input field
Name is required
</div>
</div>
Name is required
</div>
<div *ngIf="nameUser.errors?.pattern">
Name is invalid
</div>
</div>
Eg:
<div [hidden]="!nameUser?.hasError('minlength')">
This si rs required
</div>
userList:user[]=[];
//add value to userList
addUSer(newForm)
{
let user:User;
user=form.value; // get value from form
this.userList.push(user); // push to userList array
}
b.REactive Form
//component.ts
import the interface to the component
userList:user[]=[];
form:FormGroup;
ngOnInit(){
//register all controls
this.form=new FormGroup({
name: new FormControl(''); //initial data
phone: new FormControl('');
})
//submit method
which will push to userList array
<button type="submit >
//Inside TS
addUSer(form)
{
//console.log(form.value);
}
IMPORTANT:
// we can access value internal with formGroup
// her no need to pass the value for function, that's the difference.
addUSer(form)
{
//console.log(this.form.value);
this.userList.push(this.form.value);
}
this.form=new FormGroup({
name: new FormControl('',Validators.required);
})
//next add disabled in button so only if its satifies button will get enabled
eg:
ngOnInit(){
//register all controls
this.form=new FormGroup({
name: new FormControl('',[Validators.required, validators.email]);
})
//for touched
TIP:
IN CSS you can use :host it like body of the page/starting tag of that page