AngularProblems
AngularProblems
1. In browser go to https://translate.google.com/
2. Provide a word in left hands side window
3. The result of the translation is displayed in the right hand side without page reload.
4. JavaScript Object - Define JavaScript object for employee
Create a JavaScript object variable and display the content in console using console.log()
method.
Sample Output
Clicking on “Get Names” button actually retrieves user details as JSON from internet
(https://reqres.in/api/users) and displays them. Refer screen shot below.
Try accessing https://reqres.in/api/users in a browser to get a feel about the JSON data returned.
Implement changes in this code so that it displays more details about each user in the following layout:
Angular Benefits - Implement SPA using jQuery
The jQuery implementation of the previous problem is available here.
Modify the code to bring the same layout as done in the previous problem.
1. app.component.ts
2. app.component.html
3. app.module.ts
Modify app.component.html to display the message “Hello World” alone in the view.
Angular Benefits - Implement SPA using Angular
The solution for the previous problem is implemented using angular and is available here.
1. app.component.html
2. app.component.ts
3. user.service.ts
4. user.ts
let
const
Arrow Functions
Classes
Use the “Try it Yourself” option to demonstrate how each feature works.
https://www.w3schools.com/js/js_es6.asp
Reference: https://www.typescriptlang.org/docs/handbook/typescript-in-5-minutes.html
employee.ts
An interface with name Employee.
Properties - id, name, salary, permanent with data types number, string,
number and boolean types respectively.
employee-test.ts
TypeScript file to test the usage of Employee interface. Implement steps
specified below:
employee-test.js
Compile employee-test.ts to generate this file.
employee-test.html
Includes employee-test.js that displays the console output in browser.
Troubleshooting Tips
To avoid runtime error include the following line above the script definition
tags in employee-test.html.
<script> var exports = {}; </script>
<script src="employee-test.js"></script>
department.ts
An interface to represent Department.
Properties – id and name with data types number and string respectively
employee.ts
Import department and include it as attribute.
employee-test.ts
Include code to display department id and name using console.log.
TypeScript data types and interface – Include Skill details in Employee and
display
Make changes in the previous program to include skill details in employee.
skill.ts
An interface to represent Skill.
Properties – id and name with data types number and string respectively.
employee.ts
Import skill and include an array of skills as attribute.
employee-test.ts
Include code to display all skill id and skill name by iterating through the skills.
TypeScript data types and interface – Use class to display employee details
Display Employee details using a class following the steps below:
Test Cases
for .. of
https://www.typescriptlang.org/docs/handbook/iterators-and-generators.html
Explain the code under ‘for .. of statements’ section.
1. Create a new application named “angular-learning” using ng new. [NOTE: Select routing support
option during application creation]
2. Run the application in browser using ng serve command
3. Stop the service using Ctrl+C keyboard shortcut in command line
4. Test the application using ng test command in the command line
5. This will launch the test results in a browser window.
1. Open app.component.spec.ts
2. Modify the title value in line 22 to fail the test case. Change angular-learning to angular-learn and
save.
3. The test results in browser will display failed error test case details.
4. Revert the code back, so that all test cases succeed.
5. SME to walkthrough the code in app.component.spec.ts.
Karma Testing
Include test case to validate if favorite movie property and HTML element value matches.
Use 'expect' function of Karma to perform the validation. 'toEqual' of expect function can be used
to do the data comparison.
The sample snippet as shown below can be used to find the HTML element, h1.
o const compiled = fixture.debugElement.nativeElement;
o compiled.querySelector('h1').textContent
Interpolation – Display Employee details
Modify angular-learning application to display employee’s id, name and salary.
Sample Layout
Sample Layout
Interpolation – Display employee skill details using ngFor
Include employee skill details using ngFor directive.
Sample Layout
Sample Layout
Karma Testing
Include test case to check if the department name property and the value in HTML matches.
Use the sample code snippet as shown below to find the HTML element & perform the
comparison with model property
o const compiled = fixture.debugElement.nativeElement;
o compiled.querySelector('#departmentName').textContent
o component.employee.department.name
Pipes – Display employee date of birth and salary in custom format
Display Salary in currency format with any currency symbol of your choice.
This now has to be moved to a new component named "view-emp". Follow the steps below:
Create a new component in angular-learning application created earlier with component name
"view-emp".
Copy and paste relevant code from html and component source from app component to this new
component.
Create another component "edit-emp" to have a placeholder for another component that will be
developed later. This is primarily to check how router navigation works.
Include routing with router link “View Employee” and "Edit Employee" pointing to this new
component. Clicking either one of the link should display the respective component. Refer sample
layout below.
If required, use the CSS below to underline the component title that is currently active. The class active-
link is the one that needs to be applied in routerLinkActive.
body {
font-family: Segoe UI, Arial
}
a:visited {
color: blue
}
a {
text-decoration: none
}
.active-link {
border-bottom: Solid 1px blue
}
As part of this particular exercise we will create a button named "Click me". Clicking on this button should
display the message "Click me button clicked!"
In this component include a separate router link as per the layout below.
Karma Testing
Test if button click displays the message "Click me button clicked!"
Include below imports:
imports:[FormsModule]
debugElement = fixture.debugElement;
Get value from DOM using debugElement. Below code gets the button element from DOM:
debugElement.nativeElement.querySelector('#simpleClick').textContent
Event Handling and Two Way Binding - increment value in textbox based
on button click
Component: quantity-selector
Modify the “Click me” button as “Add”. Include a new textbox with default value zero. Clicking on the “Add”
button should increment value in the textbox by 1.
Sample Layout
Event Handling and Two Way Binding – Implement quantity selector for
Food App
Find below a sample layout for selecting quantity in a\ food app.
This provides option to choose the quantity using a textbox having a plus icon in the right hand side and
minus icon in the left hand side.
Clicking plus should increment the value. Clicking minus should decrement the value.
Sample Layout
Karma Testing
1
Template Driven Form Scores
Objectives
In this hands, we will create a new component and add the name field for employee.
Refer below code to include the form element and map it to the form control
Set a default value for the field by changing the name property as specified below.
Sample Layout
Reactive Form - Include form group and validation for name field
Instructions for getting employee data from instance variable and include validations:
Include getter method for name. This is for the validator to retrieve the value in the form field.
Modify template with inclusion of FormGroup and use the template driven form validation code.
Utilize the HTML template code implemented in template driven form including display of
employee JSON and form JSON
Define department as FormControl
Define the department drop down in HTML template as specified below:
<div>
<h3>Department</h3>
<select formControlName="department">
<option *ngFor="let department of departments"
[ngValue]="department"
[selected]="department.id === employee.department.id">
{{department.name}}
</option>
</select>
</div>
NOTE: No output is expected as part of this hands on and this is only for problem definition.
Create a property named searchKey and incorporate two way binding with search text box
Incorporate keyup event in the search text box point to a function named search()
Implement search() method that will filter using the filter() method of employeeList array with case
insensitive comparision of the searchKey and name in the array.
The result of the filter() method should be assigned to a component property named
'filteredEmployees'
The *ngFor in HTML template should be using filteredEmployees instead of employeeList
Sample Layout
Component Interaction - Create Component and Display Employee List
Implement the below steps for creating a component and display employee list:
Sample Layout
Parent Component will be employee-list - Contains the page heading and search text box.
Child Component will be employee-info - Contains the view for displaying a single employee
Refer screen layout below that contains details about the component details included in red.
Follow steps below to incorporate above mentioned changes:
<a [routerLink]="['/edit-emp',employee.id]">Edit</a>
Include this component app selector in employee-list component HTML. This will display a single
employee in employee-list component.
Modify the app selector in previous step to be included into *ngFor and pass the employee
reference as input to employee-info component
Include @Input decorator in employee-info component for the employee property
In employee-info.component.html modify edit link with router link. Refer code below.
<a [routerLink]="['/edit-emp-reactive',employee.id]">Edit</a>
In edit-emp-reactive.component.ts incorporate the following changes
o Import ActivatedRoute
Get the employee id parameter from the router link. Include the below code in ngOnInit() method.
Log the employeeId using console.log() to verify if we have got the employeeId appropriately.
IMPORTANT NOTE
Now we need to retrieve the specific employee from the employee list based on employeeId, so that we
can populate edit employee form. Again we are supposed to copy the hardcoded employee array from
employee-list component. Instead we will use Service feature available in angular to achieve this. Using
services we can use this data cross components. The following practice problems will help us implement
Services after which we will complete the implementation of Edit Employee component.
Create a new service using following command. Execute this command in the root folder of the
application.
Guard - AuthService
Implement AuthService that will be used to store and retrieve the authentication status. Follow steps
below to implement and use it:
Steps to implement:
createUser() - Create relevant JSON data required and invoke the POST CREATE method in
reqres.in
upateUser() - Create relevant JSON data required and invoke the PUT UPDATE method in
reqres.in
deleteUser() - Create relevant JSON data required and invoke the DELETE method in reqres.in
In the user component created in the previous step create buttons to invoke the above service methods
and display the reponse in the component's HTML template.
Test Cases
Include a method in service named register() which will make the above call and fail. Write necessary
error handler to get the error message.
Include a button named "Register" of user component to invoke the above service method and display the
error message returned in the HTML template.
Test Cases
Protractor - Setup
Follow instructions below to setup protractor:
Open google chrome and find the browser version using Menu > Help > About Google Chrome.
Copy the version number and have it ready during subsequent steps.
Execute the following commands in command line. This will install protractor and starts the
webdriver-manager. Don’t forget to change the browser version number.
conf.js
exports.config = {
seleniumAddress: 'http://localhost:4444/wd/hub',
specs: ['todo-spec.js']
};
Execute the below command in command line in the folder where .js files were placed:
protractor conf.js
Proctrator - Test Google
Test if google search website works correctly using Protractor.
Open IIS
Create a Website pointing the physical directory to the application folder inside the 'dist' folder of
the application that is being worked on
Provide a new port address other than the default 80 in the Website bindings
Browse the site to view and test