Beruflich Dokumente
Kultur Dokumente
Angular JS :-
1)Component based development With Object Oriented Programming is not
Possible.
2) No DataType Support, with Compile time type Checking is not Possible.
3) Can be used for only Client Side Programming.
4) AngularJS Supports only JavaScript/EcmaScript 5.
5) Angular Js was not built with mobile support .
Angular Interview Q's
3) What is NPM?
Ans) NPM Stands for Node Package Manager
NPM is a command line interface program to manage node.js libraries such as
package installation, version management, and dependency management.
1. Declarations: wecan list the components,directives andpipes that are part of the
module in thedeclarations array
2. Imports:we canimport other modules by listing them in the imports array
3. Providers: wecanlist the services that are partof the module in the providers array
Example:
import { Component } from'@angular/core';
@Component({
selector: 'racha-component',
template: `<h1>{{title}}</h1>
<img [src] = "myImage">`
})
export class RaChaComponent {
public myImage = "http://charanIInfo.com/images/slides/logo.jpg";
}
Angular Interview Q's
Example:
import { Component } from '@angular/core';
@Component({
selector: 'racha-component',
template: `UserName: <input type="text" #userInput><br/><br/>
<button (click)="onClick(userInput.value)">Click Here</button>`,
})
export class RaChaComponent {
onClick(value:any) {
alert(value)
}
}
Example:
import { Component } from '@angular/core';
@Component({
selector: 'racha-component',
template: `<div *ngIf="showElement">Show this content</div>
Angular Interview Q's
<div [ngSwitch]="c ourse">
<p *ngSwitchCase="'java'">Java course selelected</p>
<p *ngSwitchCase="'angular'">Angular course selelected</p>
<p *ngSwitchDefault>Invalid Course</p>
</div>
<ul>
<li *ngFor="let name of names">{{name}}</li>
</ul>`
})
export class RaChaComponent {
Component is a simple class which is associated with angular template and business logic.
Example:
@Component({
selector: 'racha-component',
template: `<h1>RaCha componentresult</h1>`,
styles: [`h1{color:orange}`]
})
export class RaChaComponent { }
Angular Interview Q's
Step2: Registration of the component
Note: We need to import and register above component in app.module.ts
stylesUrl :-
-------------
1) stylesUrl can be used to specify component style within css files.
2) stylesUrl provide intellisense and auto-completion.
3) stylesUrl can be reusable through components.
Example:
import { Component } from '@angular/core';
@Component({
selector: 'racha-component',
template: `
<div [ngClass]="['bold-text', 'mygreen']">array of classes</div>
<div [ngClass]="'italic-text myblue'">string of classes</div>
<div [ngClass]="{'small-text': c1, 'red': c2}">object of classes</div>
<div [ngStyle]="{'color': col, 'font-size': font}">style using ngStyles </div>
`,
styles:[]
})
export class RaChaComponent {
public c1: boolean = true;
public c2: boolean = true;
public col:string ='red';
Angular Interview Q's
public font:string ='30px';
}
Style.css
@Output:
-------------
@Output decorator binds a property of a component to send data from one
component (child component) to calling component (parent component).
This is one way communication from child to parent component
We will create the side-bar-toggle component which will have the side-bar
component as input and by clicking on the toggle button we will open/close
the side-bar component.
Example:
app.component.html:
app component
<app-side-bar-toggle [sideBar]="sideBar"></app-side-bar-toggle>
<app-side-bar #sideBar></app-side-bar>
side-bar-toggle.component.ts:
@Component({
selector: 'app-side-bar-toggle',
templateUrl: './side-bar-toggle.component.html',
styleUrls: ['./side-bar-toggle.component.css']
})
export class SideBarToggleComponent {
@Input() sideBar: SideBarComponent;
@HostListener('click')
click() {
this.sideBar.toggle();
}
}
side-bar.component.ts:
@Component({
selector: 'app-side-bar',
templateUrl: './side-bar.component.html',
styleUrls: ['./side-bar.component.css']
Angular Interview Q's
})
export class SideBarComponent {
@HostBinding('class.is-open')
isOpen = false;
toggle() {
this.isOpen = !this.isOpen;
}
}
Example:
app.component.html:
<app-side-bar-toggle (toggle)="toggleSideBar()"></app-side-bar-toggle>
<app-side-bar [isOpen]="sideBarIsOpened"></app-side-bar>
app.component.ts:
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
sideBarIsOpened = false;
toggleSideBar(shouldOpen: boolean) {
this.sideBarIsOpened = !this.sideBarIsOpened;
}
Angular Interview Q's
}
side-bar-toggle.component.ts:
@Component({
selector: 'app-side-bar-toggle',
templateUrl: './side-bar-toggle.component.html',
styleUrls: ['./side-bar-toggle.component.css']
})
export class SideBarToggleComponent {
@Output() toggle: EventEmitter<null> = new EventEmitter();
@HostListener('click')
click() {
this.toggle.emit();
}
}
side-bar.component.ts:
@Component({
selector: 'app-side-bar',
templateUrl: './side-bar.component.html',
styleUrls: ['./side-bar.component.css']
})
export class SideBarComponent {
@HostBinding('class.is-open') @Input()
isOpen = false;
side-bar-toggle.component.ts:
@Component({
selector: 'app-side-bar-toggle',
templateUrl: './side-bar-toggle.component.html',
styleUrls: ['./side-bar-toggle.component.css']
})
export class SideBarToggleComponent {
constructor(
private sideBarService: SideBarService
){}
@HostListener('click')
click() {
this.sideBarService.toggle();
}
}
side-bar.component.ts:
@Component({
selector: 'app-side-bar',
templateUrl: './side-bar.component.html',
styleUrls: ['./side-bar.component.css']
})
export class SideBarComponent {
@HostBinding('class.is-open')
isOpen = false;
Angular Interview Q's
constructor(
private sideBarService: SideBarService
){}
ngOnInit() {
this.sideBarService.change.subscribe(isOpen => {
this.isOpen = isOpen;
});
}
}
side-bar.service.ts:
@Injectable()
export class SideBarService {
isOpen = false;
@Output() change: EventEmitter<boolean> = new EventEmitter();
toggle() {
this.isOpen = !this.isOpen;
this.change.emit(this.isOpen);
}
}
Add it as a provider
Ex:
@Component({
selector: 'app-root',
template: `<h2>RaCha Infotech</h2><hr/>
<employee-list></employee-list>`,
providers: [EmployeeService]
})
App.component.html:
<div class="container">
<h2>User RegistrationForm</h2>
<form #userForm = "ngForm" (ngSubmit)="onSubmit(userForm.value)">
<div class="form-group">
<label>Name:</label><input type="text" #nameRef="ngModel" required
minlength="5" maxlength="9" pattern="[a-z]+" class="form-control"name="name"
ngModel>
<!--<b>{{nameRef.className}}</b>-->
<!--<div [hidden]="nameRef.valid | | nameRef.pristine" class="alert alert-danger">
Please Enter Name
</div>-->
<div *ngIf="nameRef.errors && (nameRef.dirty | | nameRef.touched)" class="alert
alert-danger">
<div [hidden]="!nameRef.errors.required">
Please Enter a Name
</div>
<div [hidden]="!nameRef.errors.minlength">
Please Enter atleast 5 characters
</div>
<!-- maxlength validation not required bcz we are not allowed to enter more than
that.-->
<div [hidden]="!nameRef.errors.pattern">
Please Enter validdata
Angular Interview Q's
</div>
</div>
</div>
<div class="form-group">
<label>Contact:</label><input type="text" required class="form-control"
name="contact" ngModel>
</div>
<div class="form-group">
<label>Email:</label><input type="text" requiredclass="form-control"
name="email" ngModel>
</div>
<div ngModelGroup="Address">
<div class="form-group">
<label>Street:</label><input type="text" required class="form-control"
name="street" ngModel>
</div>
<div class="form-group">
<label>City:</label><input type="text" required class="form-control"
name="city" ngModel>
</div>
<div class="form-group">
<label>PostalCode:</label><input type="text" required class="form-control"
name="postalcode" ngModel>
</div>
</div>
App.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: `app/app.component.html`,
styles:[`input.ng-invalid{border-left:5px solid red}
input.ng-valid{border-left:5px solidgreen}`]
})
export class AppComponent {
onSubmit(value:any){
console.log(value);
}
Angular Interview Q's
}
Steps:
1. Set the base Tag in index.html file
<base href="/">
2. Import RouterModule
import { RouterModule } from '@angular/router';
3. Configure routes
declarations: [AppComponent, WebComponent, JavaComponent],
RouterModule.forRoot([
Angular Interview Q's
{path:'web', component: WebComponent},
{path:'java', component:JavaComponent}
])
Note: There are Promise libraries out there that support cancellation, but ES6
Promise doesn't so far.
Observable:
----------------
An Observable is like a Stream (in many languages) and allows to pass zero or
more events where the callback is called for each event.
Observable also has the advantage over Promise to be cancelable. If the result of
an HTTP request to a server or some other expensive async operation isn't
needed anymore, the Subscription of an Observable allows to cancel the
subscription, while a Promise will eventually call the success or failed callback
even when you don't need the notification or the result it provides anymore.
Observable provides operators like map, forEach, reduce, ... similar to an array
There are also powerful operators like retry(), or replay(), ... that are often
quite handy.
Angular Interview Q's
Architecture:
Annotations :−
-------------------
Angular Interview Q's
These are decorators at the class level. This is an array and an example having
both the @Component and @Routes decorator.
app.component.ts file:-
@Component ({
selector: 'my-app',
templateUrl: 'app/app.component.html'
})
The component decorator is used to declare the class in the app.component.ts file as a
component.
This guide explains how to specify metadata and apply available compiler
options to compile your applications efficiently using the AOT compiler.
JIT compilation is the default when you run the ng build (build only) or ng serve (build and
serve locally) CLI commands:
ng build
ng serve