Skip to content

Commit

Permalink
delete individual components
Browse files Browse the repository at this point in the history
  • Loading branch information
Niclas Timle authored and Niclas Timle committed Nov 8, 2024
1 parent 6ded524 commit 4513ccb
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 32 deletions.
7 changes: 6 additions & 1 deletion src/app/components/basicComponent/basic.component.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
<div class="d-flex flex-wrap" style="height: 300px;">
<div *ngFor="let item of sharedService.flexItems" [innerHTML]="item"></div>
<div *ngFor="let item of sharedService.flexItems"
class="action-btn"
style="height: 300px; width:330px; flex: 1 1 330px; margin: 10px; background:darkgrey;">
Flex item
<button (click)="Remove(item.id)">Remove Button </button>
</div>
</div>
4 changes: 4 additions & 0 deletions src/app/components/basicComponent/basic.component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Component } from '@angular/core';

import { CommonModule } from '@angular/common'; // Import CommonModule for *ngFor
import { SharedService } from '../../shared.service';

Expand All @@ -11,4 +12,7 @@ import { SharedService } from '../../shared.service';
})
export class BasicComponent {
constructor(public sharedService: SharedService) {}
Remove(id: number): void {
this.sharedService.Remove(id);
}
}
2 changes: 1 addition & 1 deletion src/app/components/header/header.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ <h5 class="modal-title" id="exampleModalLabel">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-primary" data-bs-dismiss="modal" (click)="addComponent()">Add new component</button>
<button type="button" class="btn btn-primary" data-bs-dismiss="modal" (click)="AddComponent()">Add new component</button>
</div>
</div>
</div>
Expand Down
7 changes: 2 additions & 5 deletions src/app/components/header/header.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ export class HeaderComponent{

constructor(public sharedService: SharedService) {}

addComponent() {
this.sharedService.addItem();
}
removeComponent() {
this.sharedService.removeItem();
AddComponent() {
this.sharedService.AddItem();
}
}
42 changes: 17 additions & 25 deletions src/app/shared.service.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,31 @@
import { Injectable } from '@angular/core';
import { DomSanitizer, SafeHtml } from '@angular/platform-browser'; // Import DomSanitizer and SafeHtml

@Injectable({
providedIn: 'root'
})

export class SharedService {
public flexItems: SafeHtml[] = [];
flexItems: { id: number }[] = [];

constructor(private sanitizer: DomSanitizer){
this.flexItems = [
this.sanitizeHtml('')
];
Remove(id: number) {
let index = this.GetId(id);
if(index > -1){
this.flexItems.splice(index, 1);
}
}

sanitizeHtml(html: string): SafeHtml {
return this.sanitizer.bypassSecurityTrustHtml(html);
AddItem() {
const newId = Math.floor(Math.random() * 100) + 1;
let index = this.GetId(newId);
console.log(index);
if(index === -1){
this.flexItems.push({ id: newId });
}
else
this.AddItem();
}

addItem() {
const newItem = this.sanitizeHtml(`<div
style="
height: 300px;
width:330px;
flex: 1 1 330px;
margin: 10px;
background:darkgrey;">Flex item ${this.flexItems.length}</div>`);
this.flexItems.push(newItem);
}
removeItem(){
if(this.flexItems.length > 1){
this.flexItems.splice(this.flexItems.length -1,1);
}
if(this.flexItems.length === 1){
this.flexItems.splice(this.flexItems.length,1);
}
GetId(id: number): number{
return this.flexItems.findIndex(item => item.id == id);
}
}

0 comments on commit 4513ccb

Please sign in to comment.