-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2875281
commit 08ecdfd
Showing
20 changed files
with
421 additions
and
356 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# Components: | ||
|
||
- Components are the main building blocks of an Angular App. | ||
- The Root Component contains a tree of Angular Components. | ||
- It is a subset of directives and always associated with a template. | ||
|
||
A Component in Angular is a TypeScript class that can contain methods and properties. This allows developers to create and bind their own functionality to the user interface. | ||
|
||
## Creating Angular Component using Angular CLI: | ||
|
||
We can create Angular Component using Angular CLI by following command: | ||
|
||
```bash | ||
ng g c <componentName> | ||
OR | ||
ng generate component <componentName> | ||
``` | ||
|
||
## Generate Demo Component | ||
|
||
To generate a demo component, you can run the following command: | ||
|
||
```bash | ||
ng generate component demo / ng g c demo | ||
``` | ||
|
||
## Files Generated After this Command Executes: | ||
|
||
- `componentName.component.ts` : This is the main class file of the component. | ||
- `componentName.component.html` : This is the template file for the component. | ||
- `componentName.component.css` : This is the CSS file for the component. | ||
- `componentName.component.spec.ts` : This is the unit test file for the component. | ||
|
||
### Example: | ||
|
||
```js | ||
@Component({ | ||
selector: 'app-my-component', | ||
templateUrl: './my-component.component.html', | ||
styleUrls: ['./my-component.component.css'] | ||
}) | ||
``` | ||
|
||
If we want to use a template in our component, we can use the `template` property in the component. We can also use the `templateUrl` property to specify the path of the template file. Similarly, we can use `styleUrls` property to specify the path of the CSS file. | ||
|
||
```js | ||
@Component({ | ||
selector: 'app-my-component', | ||
template: ` | ||
<div> | ||
<h1>{{ title }}</h1> | ||
<p>{{ description }}</p> | ||
</div> | ||
`, | ||
styles: [` | ||
div { | ||
padding: 20px; | ||
background-color: #f8f9fa; | ||
} | ||
h1 { | ||
color: #007bff; | ||
} | ||
p { | ||
color: #6c757d; | ||
} | ||
`] | ||
}) | ||
``` |
Empty file.
Empty file.
23 changes: 23 additions & 0 deletions
23
src/app/components/add-employee/add-employee.component.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
|
||
import { AddEmployeeComponent } from './add-employee.component'; | ||
|
||
describe('AddEmployeeComponent', () => { | ||
let component: AddEmployeeComponent; | ||
let fixture: ComponentFixture<AddEmployeeComponent>; | ||
|
||
beforeEach(async () => { | ||
await TestBed.configureTestingModule({ | ||
imports: [AddEmployeeComponent] | ||
}) | ||
.compileComponents(); | ||
|
||
fixture = TestBed.createComponent(AddEmployeeComponent); | ||
component = fixture.componentInstance; | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it('should create', () => { | ||
expect(component).toBeTruthy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { CommonModule } from '@angular/common'; | ||
import { Component } from '@angular/core'; | ||
|
||
@Component({ | ||
selector: 'app-add-employee', | ||
standalone: true, | ||
imports: [CommonModule], | ||
templateUrl: './add-employee.component.html', | ||
styleUrl: './add-employee.component.css', | ||
}) | ||
export class AddEmployeeComponent {} |
Empty file.
21 changes: 21 additions & 0 deletions
21
src/app/components/employee-list/employee-list.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<div class="container"> | ||
<h2>Employee List</h2> | ||
<ul class="list-group"> | ||
<li | ||
class="list-group-item d-flex justify-content-between align-items-center" | ||
*ngFor="let employee of employees" | ||
> | ||
<div> | ||
<b>Name:</b> {{ employee.name }}<br /> | ||
<b>Age:</b> {{ employee.age }}<br /> | ||
<b>Salary:</b> {{ employee.salary }} | ||
</div> | ||
<button class="btn btn-danger" (click)="deleteEmployee(employee.id)"> | ||
Delete | ||
</button> | ||
</li> | ||
</ul> | ||
<button class="btn btn-primary mt-3" (click)="employeeList()"> | ||
Add Employee | ||
</button> | ||
</div> |
23 changes: 23 additions & 0 deletions
23
src/app/components/employee-list/employee-list.component.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
|
||
import { EmployeeListComponent } from './employee-list.component'; | ||
|
||
describe('EmployeeListComponent', () => { | ||
let component: EmployeeListComponent; | ||
let fixture: ComponentFixture<EmployeeListComponent>; | ||
|
||
beforeEach(async () => { | ||
await TestBed.configureTestingModule({ | ||
imports: [EmployeeListComponent] | ||
}) | ||
.compileComponents(); | ||
|
||
fixture = TestBed.createComponent(EmployeeListComponent); | ||
component = fixture.componentInstance; | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it('should create', () => { | ||
expect(component).toBeTruthy(); | ||
}); | ||
}); |
46 changes: 46 additions & 0 deletions
46
src/app/components/employee-list/employee-list.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { CommonModule } from '@angular/common'; | ||
import { Component } from '@angular/core'; | ||
|
||
@Component({ | ||
selector: 'app-employee-list', | ||
standalone: true, | ||
imports: [CommonModule], | ||
templateUrl: './employee-list.component.html', | ||
styleUrl: './employee-list.component.css', | ||
}) | ||
export class EmployeeListComponent { | ||
employees = [ | ||
{ id: 1, name: 'John Doe', age: 25, salary: 5000 }, | ||
{ id: 2, name: 'Jane Smith', age: 30, salary: 6000 }, | ||
{ id: 3, name: 'Bob Johnson', age: 40, salary: 7000 }, | ||
]; | ||
|
||
employeeList() { | ||
const randomId = Math.floor(Math.random() * 1000) + 4; | ||
const randomNames = [ | ||
'Alice Brown', | ||
'Charlie White', | ||
'Daisy Green', | ||
'Edward Black', | ||
'Fiona Blue', | ||
]; | ||
const randomName = | ||
randomNames[Math.floor(Math.random() * randomNames.length)]; | ||
const randomAge = Math.floor(Math.random() * 30) + 20; | ||
const randomSalary = Math.floor(Math.random() * 5000) + 4000; | ||
|
||
const randomEmployee = { | ||
id: randomId, | ||
name: randomName, | ||
age: randomAge, | ||
salary: randomSalary, | ||
}; | ||
|
||
this.employees.push(randomEmployee); | ||
} | ||
deleteEmployee(employeeId: number) { | ||
this.employees = this.employees.filter( | ||
(employee) => employee.id !== employeeId | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# LifeCycle Events: | ||
|
||
A component's lifecycle is the sequence of steps that happen between the component's creation and its destruction. | ||
|
||
- Constructor | ||
- ngOnInit | ||
- ngDoCheck | ||
- ngAfterContentInit | ||
- ngAfterContentChecked | ||
- ngAfterViewInit | ||
- ngAfterViewChecked | ||
- ngOnDestroy | ||
- ngChanges | ||
|
||
## Constructor | ||
|
||
- We use it for initialization of Variables in a Component. | ||
|
||
## OnInit | ||
|
||
- Called when the component is initialized | ||
- Called automatically after the component is loaded. | ||
- Normally it is used for setup tasks like fetching data from an API. | ||
|
||
## DoCheck | ||
|
||
- Called when the component is checked/ change is detected. | ||
|
||
## AfterContentInit | ||
|
||
- Called when angular projects external content into Component's view. | ||
|
||
## AfterContentChecked | ||
|
||
- Called when angular checks the content projected into Component's view. | ||
|
||
## AfterViewInit | ||
|
||
- Called after angular initializes the component's view, child's view, or the view that contains the directive. | ||
|
||
## AfterViewChecked | ||
|
||
- Called after angular checks the component's view, child's view, or the view that contains the directive. | ||
|
||
## OnDestroy | ||
|
||
- Cleanup just before angular destroys the component, Unsubscribe Observables & detach event handlers to avoid memory leaks. | ||
|
||
## OnChanges | ||
|
||
- Called when any data-bound property of a directive changes. | ||
- It is called after ngOnInit and ngDoCheck and is used in the reuseable components |
Empty file.
Oops, something went wrong.