Professional Documents
Culture Documents
CS17505 - Question Bank - Unit 5 - Angular 4: Difference Between Ng-Class and Ng-Style
CS17505 - Question Bank - Unit 5 - Angular 4: Difference Between Ng-Class and Ng-Style
Part A
Part B
Components:
Components are the most important basic UI building blocks for an Angular project. It is used to
design and build an application. Components are basically classes that interact with the .html file of
the component, which gets displayed on the browser. A Component is a TypeScript class where we
can create our own methods and properties as per your requirement which is used to bind with a UI
(html page) of our application.
Default Components
1. app.component.css – Style sheet of a component
2. app.component.html – HTML web page view of a component
3. app. Component.spec.ts – Debug file used for unit testing
4. app.component.ts – class file to define modules, properties, etc
5. app.module.ts – to bind the components, directives, pipes and services which are related to
the application
Example: Refer PPT
Templates
Templates separates view layer from the rest of the framework. We can change the view layer
without breaking the application. We can define a template in two ways
Inline Template External Template
Inline style into template - <style> <style> tag added in component.html file
e2e − end to end test folder. Mainly e2e is used for integration testing and helps ensure the
application works fine.
node_modules − The npm package installed is node_modules. You can open the folder and see the
packages available.
src − This folder is where we will work on the project using Angular 4.
File structure
.angular-cli.json − It basically holds the project name, version of cli, etc.
.editorconfig − This is the config file for the editor.
.gitignore − A .gitignore file should be committed into the repository, in order to share the ignore
rules with any other users that clone the repository.
karma.conf.js − This is used for unit testing via the protractor. All the information required for the
project is provided in karma.conf.js file.
package.json − The package.json file tells which libraries will be installed into node_modules when
you run npm install.
3. Write notes on in-built directives. Explain the steps to create custom directives with sample
code?
Directives are used to change the behavior of components or elements.
Types: 1. Component, 2. Structural and 3. Attribute Directive
Components Directive - Components are directives with a template or view. @Component
decorator is actually @Directive with templates. how the component should be processed,
instantiated and used at runtime.
Structural Directive - deals with manipulating DOM elements *directive-name = expression
(i) *ngIf - add/remove elements in DOM tree based on criteria
Ex: <div *ngIf="courses.length >0">List of Courses</div>
(ii) *ngFor – use for iterating the elements
<tr *ngFor='let c of cricketers let i=index’>
<td>{{i+1}}</td>
<td>{{c.name}}</td> <td>{{c.ODI}}</td>
<td>{{c.Test}}</td>
</tr>
(iii) *ngSwitch – used to check for matching cases.
<div [ngSwitch]="Pselection">
<p *ngSwitchCase="'SG'">Sourav selected</p>
<p *ngSwitchCase="'ST'">Sachin selected</p>
<p *ngSwitchCase="'RD'">Rahul selected</p>
<p *ngSwitchDefault>NO player selected</p>
</div>
Attribute directive - is used to change/modify the behavior of the HTML element in the Dom
Layout. Types: ngStyle, ngClass
(i) ngStyle – ngStyle is used to set multiple inline styles for the HTML element.
ng-style="{color: 'red'}"
4. Compare template driven forms and reactive forms. Write the code to create a simple
template form with input tags having email id, password and the submit button.
Forms play an important role in web applications and enable the users to perform data entry task.
Template driven Forms - Used to create small to medium sized forms. Entire form validation is
done in HTML templates.
Model driven Forms or Ractive Forms - Used to create large size forms. Entire form validation is
done in Component class using FormBuilder and Validators classes.
<form #empform="ngForm" (ngSubmit)="abc(empform)" novalidate>
Email id <input type="text" name="fname" ngModel required ><br>
Password <input type="text" name="pwd" ngModel required ><br>
<button class="btn btn-primary" type="submit”'> Save </button>
</form>
5. What is the use of services in Angular 4? Write the step by step process to create a service
and create a function which will display today’s date.
A service in Angular is a class which contains some functionality that can be reused across the
application. Components use to display and present the data. Services use to fetch the data. We will
simply create a service and it will be used by the component where we want the data. Purpose of
service might be one of the following:
To write the business logic.
To provide shared data to the components.
To handle the external interactions, like to get the data from API.
How to create a service?
1. ng generate service (name of service)
Example: >ng generate service studentdata
It will create the following files.
studentdata.service.ts
studentdata.service.spec.ts
2. Now, import the service file into the app.module.ts file
import {StudentdataService} from './studentdata.service';
@NgModule({
providers: [StudentdataService],
})
3. Add some code to the studentdata.service.ts file. This file contains the data that we need to
consume. So this is service file. So this file provides services from frontend to backend.
4. Use services into the component app.component.ts
5. update the HTML app.component.html
Example: Refer PPT – slide 51
const routes: Routes = [
{path: 'home',component:HomeComponent},
{path: 'courses', component:CoursesComponent},
{path: 'students', component:StudentsComponent},
{path: '',redirectTo:'/home', pathMatch:'full'} ];
export class AppRoutingModule { }
export const routingComponents=[HomeComponent,CoursesComponent,StudentsComponent]
app.component.html
<h1>welcome to Angular Routing</h1>
<ul>
<li><a routerLink="/home">Home</a></li>
<li><a routerLink="/courses">Courses</a></li>
<li><a routerLink="/students">Students</a></li>
</ul>
<router-outlet></router-outlet>