Wednesday 26 September 2018

Angular Code Questions

Q: How to get data from url in Angular 
http://localhost:4200/about/7

const routesRoutes = [
    { path: ''component: DatabindingComponent },
    { path: 'about/:id'component: AboutComponent },
    { path: 'notfound'component: NotfoundComponent },
    { path: '**'redirectTo: 'notfound' }
   ];

export class AboutComponent implements OnInit {
    idstring;  id2string;
    constructor(private routeActivatedRoute) { }
    
    ngOnInit() {
       this.route.params.subscribe((params=> {
       this.id = params.id;
this.id2 = this.route.snapshot.params.id;
      });
    }

Q: Data Binding in Angular

databinding.component.ts
export class DatabindingComponent implements OnInit {
    companystring;
    urlstring;
    name:string;
    constructor() {}
    
    ngOnInit() {
    this.company = 'Dot Net Guru';
    this.url = 'http://www.dotnetguru.in';
    this.name='Suraj';
    }
    
    Greet() {
     console.log(this.name);
     alert('Hi from Angular!');
     }
    }

databinding.component.htm
//Interpolation
<p>{{company}}, {{name}}</p>
<img src='{{imagePath}}'/>
//Property Binding
<a [href]="url">Website</a>
<img [src]='imagePath'/>
<input type="text" [value]="name"/>
//Event Binding
<button type="button" (click)="Greet()">Greet</button>
//Two Way data binding
<input type="text" [(ngModel)]="name"/

Angular attribute binding
<tr><th [attr.colspan]="columnSpan">Employee Details</th></tr>

Angular class binding: https://www.youtube.com/
Angular style binding: https://www.youtube.com/

Q: Directive (If, If-else, loop, switch-case)

dir.component.ts
export class DirComponent implements OnInit {
    numnumber;
    alphabet:string;
    colors=['red','blue','green'];
    
    constructor() {}
    ngOnInit() {
       this.num = 1;
      }
    }

dir.component.html
<p>
<input type="number" [(ngModel)]="num" />
</p>

<h3>ngIf</h3>
<div *ngIf="num%2==0">
Even Number
</div>

<div *ngIf="num%2>0">
Odd Number
</div>

<h3>ngIf..else</h3>
<div *ngIf="num%2==0 then divif; else divelse"></div>
<ng-template #divif>Even Number</ng-template>
<ng-template #divelse>Odd Number</ng-template>

<h3>ngSwitch</h3>
<p>
<input type="text" [(ngModel)]="alphabet" />
</p>

<div [ngSwitch]="alphabet">
<div *ngSwitchCase="'a'">Vowel</div>
<div *ngSwitchCase="'e'">Vowel</div>
<div *ngSwitchCase="'i'">Vowel</div>
<div *ngSwitchCase="'o'">Vowel</div>
<div *ngSwitchCase="'u'">Vowel</div>
<div *ngSwitchDefault>Not Vowel</div>
</div>

<h3>Loop</h3>
<ul>
<li *ngFor="let item of colors; let i=index">
{{i+1}} {{item}}
</li>
</ul>
Output: 

Q: Pipe
<tr *ngFor="let item of emp | orderBy: 'name' : true" >
<td>{{item.name | uppercase}}</td>
<td>{{item.address | lowercase | reverse}}</td>
<td>{{item.joining | date:'dd/MM/yyyy'}}</td>
<td>{{item.salary | currency:'INR'}}</td>
</tr>

Q: Access data from parent to child component

Parent Component
@Component({
    selector: 'app-master',
    templateUrl: './master.component.html',
    styles: []
  })
  export class MasterComponent implements OnInit {
    companystring;
    constructor() {
      this.company = 'Dot Net Tricks';
    }
    ngOnInit() {}
  }

Parnet Component html
<app-details [companyName]="company"></app-details>

Child Component
import { ComponentOnInitInputOutputfrom '@angular/core';

@Component({
  selector: 'app-details',
  templateUrl: './details.component.html',
  styles: []
})
export class DetailsComponent implements OnInit {
  @Input() companyNamestring;
  constructor() { }

  ngOnInit() {}
}

Child Component html
<p>{{companyName}}</p>

Q: Access data from child to parent component

Child Component html
<button type="button" (click)="SendMessage()">Send Msg</button>

Child Component
import {Component,OnInit,Output,EventEmitterfrom '@angular/core';
@Component({
  selector: 'app-details',
  templateUrl: './details.component.html',
  styles: []
})
export class DetailsComponent implements OnInit {
@Output() sendMsgEventEmitter<string>=new EventEmitter<string>();
  constructor() {}
  ngOnInit() {}

  SendMessage() {
    this.sendMsg.emit('Message from Child');
  }
}

Parent Component html
<h2>Parent Component : {{msg}}</h2>
<app-details [companyName]="company" (sendMsg)="ReceivedMsg($event)"></app-details>

Parent Component 
import { ComponentOnInit } from '@angular/core';

@Component({
  selector: 'app-master',
  templateUrl: './master.component.html',
  styles: []
})
export class MasterComponent implements OnInit {
  msgstring;
  constructor() {}
  ngOnInit() {}
  ReceivedMsg(msgstring) {
    this.msg = msg;
    alert(msg);
  }
}

Q: What is the equivalent of ngShow and ngHide in Angular?
Just bind to the hidden property.
[hidden]="!myVar"

*ngIf vs [hidden]
*ngIf effectively removes its content from the DOM while [hidden] modifies the display property and only instructs the browser to not show the content but the DOM still contains it.

0 comments:

Post a Comment

Topics

ADFS (1) ADO .Net (1) Ajax (1) Angular (43) Angular Js (15) ASP .Net (14) Authentication (4) Azure (3) Breeze.js (1) C# (47) CD (1) CI (2) CloudComputing (2) Coding (7) CQRS (1) CSS (2) Design_Pattern (6) DevOps (4) DI (3) Dotnet (8) DotnetCore (16) Entity Framework (2) ExpressJS (4) Html (4) IIS (1) Javascript (17) Jquery (8) Lamda (3) Linq (11) microservice (3) Mongodb (1) MVC (46) NodeJS (8) React (11) SDLC (1) Sql Server (32) SSIS (3) SSO (1) TypeScript (1) UI (1) UnitTest (1) WCF (14) Web Api (15) Web Service (1) XMl (1)

Dotnet Guru Archives