Angular App Laptop Services
Angular App Laptop Services
using services
@Injectable({
providedIn: 'root'
})
laptops = [
];
constructor() { }
getLaptops() {
return this.laptops;
getLaptop(id: number) {
addLaptop(laptop) {
this.laptops.push(laptop);
updateLaptop(laptop) {
this.laptops[index] = laptop;
deleteLaptop(id: number) {
this.laptops.splice(index, 1);
@Component({
selector: 'app-laptop',
template: `
<ul>
</li>
</ul>
`,
})
laptops = [];
ngOnInit() {
this.laptops = this.laptopService.getLaptops();
addLaptop() {
this.laptopService.addLaptop(newLaptop);
this.laptops = this.laptopService.getLaptops();
@Component({
selector: 'app-add-laptop',
template: `
<form (ngSubmit)="addLaptop()">
<label>Name:</label>
<br>
<label>Price:</label>
<br>
</form>
`,
})
ngOnInit() {
addLaptop() {
this.laptopService.addLaptop(this.newLaptop);
}
}
@Component({
selector: 'app-edit-laptop',
template: `
<form (ngSubmit)="updateLaptop()">
<label>Name:</label>
<br>
<label>Price:</label>
<br>
</form>
`,
})
ngOnInit() {
}
updateLaptop() {
this.laptopService.updateLaptop(this.laptop);
@Component({
selector: 'app-delete-laptop',
template: `
`,
})
laptopId = 1;
ngOnInit() {
deleteLaptop() {
this.laptopService.deleteLaptop(this.laptopid);