Posts

what is observable in angular? promise vs observable.

Promise is active, whereas observable is lazy.  It means promise completes its next task till response comes. whereas observable do nothing and waits for the response. Promise is always asynchronous(start to run next code till response), whereas an Observable is default synchronous(don't run next code and wait for response) but you can make it asynchronous(you can forcefully make it asynchronous). Promise provide values only one time when you call the service. but Observable can continuously send value(notifications) on on fix time interval. for that you will have to set interval using "setInterval" function.     

how to delete selected objects from array by key value. javascript

//main array list arrayList = [{'id':1,'name':'salman',age:'22'},{'id':2,'name':'Adam',age:'21'},{'id':3,'name':'John',age:'23'}]; //list of object ids to be deleted idList [id:1,id:3]; //delete method idList.forEach(f => arrayList.splice(arrayList.findIndex(e => e.id === f.id),1));  console.log(arrayList); ----Output----  [{'id':2,'name':'Adam',age:'21'}];

how to save images with angular nodejs

 There are two ways to save images. 1) store image in database by converting it to binary format. 2) copy image in your project folder then save image path in table. ------- 1st way with front end angular------ pass uploaded file in "handleFileInput" function.  saveImage ( files :  FileList ) {    var   file : File  =  files . item ( 0 );    var   myReader : FileReader  =  new   FileReader ();    myReader . onloadend  = ( e )  =>  {      this . image  =  myReader . result ;   }    myReader . readAsDataURL ( file );    //convert to base64 ends } now you will get  binary format image in this.image variable. save this converted image directly to database through API. ------2nd way with api nodejs------ we can use "multer" to copy file in project folder. const   multer  =  require ( 'multer'...

How to filter objects from array javascript

 var products = [{"_id":1,"name":"product1 ","description":"good product"}, {"_id":2,"name":"product2 ","description":"good product"}, {"_id":3,"name":"product3 ","description":"good product"}, {"_id":4,"name":"product4 ","description":"good product"}, {"_id":5,"name":"product5 ","description":"good product"}]  var newArray = products.filter(function (el) {                 return el._id > 2;                }); -------Output------ [{"_id":3,"name":"product3 ","description":"good product"}, {"_id":4,"name":"product4 ","description":"good product"}, {"_id":5,"name":"product5 ","description":"good p...

important commands for angular CLI

1) Create Module :- ng g module ModuleName 2) Create Module with Routing :-  ng g module ModuleName --routing 3) Create Component :- ng g c ComponentName 4) Create Lazy Loading Module with Routing and Component :-  ng generate module ModuleName --route RouteLinkName --module app.module -->  RouteLinkName  is just a name   it can be anything whatever you wish.   

How to scroll page to Top on route change.

Add bellow code in app.component.ts file. this . router . events . subscribe (( evt ) => { if (!( evt instanceof NavigationEnd )) { return ; } window . scrollTo ( 0 , 0 ) }); app.component.ts import { Component } from '@angular/core' ; import { Router , NavigationEnd } from '@angular/router' ; @ Component ({ selector: 'app-root' , templateUrl: './app.component.html' , styleUrls: [ './app.component.css' ] }) export class AppComponent { title = 'AngularCrud' ; constructor ( private router : Router ) { } ngOnInit () { this . router . events . subscribe (( evt ) => { if (!( evt instanceof NavigationEnd )) { return ; } window . scrollTo ( 0 , 0 ) }); } }

how to create custom validator function

Step 1 :  Create the custom validator function   function  emailDomain(control: AbstractControl): { [key: string]: any } | null {    const  email: string = control.value;    const  domain = email.substring(email.lastIndexOf( '@' ) +  1 );    if  (email ===  ''  || domain.toLowerCase() ===  'gmail.com' ) {      return   null ;   }  else  {      return  {  'emailDomain' :  true  };   } } Step 2 :  Attach the custom validator function to the control that we want to validate email: [ '' , [ Validators .required,  emailDomain ]] Step 3 :  Display the validation error message <span  * ngIf = "employeeForm.get('email').errors.emailDomain" >   Email domian should be gmail.com </span>