Posts

How to restrict your logged In page in angular (authentication guard)

 You can set authentication guard to restrict users which are not loggedin. 1) Service of auth guard    import { Injectable } from '@angular/core' ; import { Router , CanActivate , ActivatedRouteSnapshot , RouterStateSnapshot , UrlTree } from '@angular/router' ; import { CookieService } from 'ngx-cookie-service' ; @ Injectable ({ providedIn: 'root' }) export class AuthGuardService implements CanActivate { constructor ( private _router : Router , private _cookieService : CookieService ) { } userid = "" ; canActivate ( route : ActivatedRouteSnapshot , state : RouterStateSnapshot ): boolean | UrlTree { this . userid = this . _cookieService . get ( 'userid' ) if ( this . userid == "" ) { alert ( 'You are not allowed to view this page. You are redirected to login Page' ); this . _router . navigate ([ "login" ],{ queryParams: { retUrl: route . url } }); return false ; /...

How to notify about data update to other component.

   we can update about data changes to any component using bellow simple method 1) Service to publish and receive data import {Injectable} from '@angular/core'; import {Subject} from 'rxjs'; @Injectable({ providedIn: 'root' }) export class GlobalFooService { private fooSubject = new Subject<any>(); publishSomeData(data: any) { this.fooSubject.next(data); } getObservable(): Subject<any> { return this.fooSubject; } } 2) Data sending component @Component({ selector: 'app-home', templateUrl: 'home.page.html', styleUrls: ['home.page.scss'] }) export class HomePage { constructor(private globalFooService: GlobalFooService) { } onSomeButtonClick() { this.globalFooService.publishSomeData({ foo: 'bar' }); } } 3) Data receiving component @Component({ selector: 'app-root', templateUrl: 'app.component.htm...

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.