Skip to content

Commit 25ea774

Browse files
Cibele MartinsCibele Martins
authored andcommitted
🚧 feat: injecting a service into another service
1 parent 72b0c70 commit 25ea774

File tree

5 files changed

+23
-6
lines changed

5 files changed

+23
-6
lines changed

src/app/account.service.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
// injectable
2+
import { Injectable } from "@angular/core";
3+
4+
// other service
5+
import { LoggingService } from "./logging.service";
6+
7+
@Injectable()
8+
19
export class AccountService {
210

311
accounts = [
@@ -15,11 +23,17 @@ export class AccountService {
1523
}
1624
];
1725

26+
constructor(private logging: LoggingService) {
27+
28+
}
29+
1830
onAccountAdded(name: string, status: string) {
1931
this.accounts.push({name: name, status: status});
32+
this.logging.changeStatus(status)
2033
}
2134

2235
onStatusChanged(id: number, newStatus: string) {
2336
this.accounts[id].status = newStatus;
37+
this.logging.changeStatus(newStatus)
2438
}
2539
}

src/app/account/account.component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { AccountService } from '../account.service';
66
selector: 'app-account',
77
templateUrl: './account.component.html',
88
styleUrls: ['./account.component.css'],
9-
providers: [LoggingService]
9+
// providers: [LoggingService]
1010
})
1111
export class AccountComponent {
1212
@Input() account: {name: string, status: string};
@@ -18,7 +18,7 @@ export class AccountComponent {
1818

1919
onSetTo(status: string) {
2020
this.accountService.onStatusChanged(this.id, status)
21-
this.logging.changeStatus(status)
21+
// this.logging.changeStatus(status)
2222
}
2323

2424
}

src/app/app.component.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { AccountService } from './account.service';
55
selector: 'app-root',
66
templateUrl: './app.component.html',
77
styleUrls: ['./app.component.css'],
8-
providers: [AccountService]
98
})
109
export class AppComponent {
1110

src/app/app.module.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import { AppComponent } from './app.component';
77
import { AccountComponent } from './account/account.component';
88
import { NewAccountComponent } from './new-account/new-account.component';
99

10+
// servic es
11+
import { AccountService } from './account.service';
12+
import { LoggingService } from './logging.service';
13+
1014
@NgModule({
1115
declarations: [
1216
AppComponent,
@@ -17,7 +21,7 @@ import { NewAccountComponent } from './new-account/new-account.component';
1721
BrowserModule,
1822
FormsModule,
1923
],
20-
providers: [],
24+
providers: [AccountService, LoggingService],
2125
bootstrap: [AppComponent]
2226
})
2327
export class AppModule { }

src/app/new-account/new-account.component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { AccountService } from '../account.service';
66
selector: 'app-new-account',
77
templateUrl: './new-account.component.html',
88
styleUrls: ['./new-account.component.css'],
9-
providers: [LoggingService]
9+
// providers: [LoggingService]
1010
})
1111
export class NewAccountComponent {
1212

@@ -22,6 +22,6 @@ export class NewAccountComponent {
2222

2323
this.accountService.onAccountAdded(accountName, accountStatus)
2424

25-
this.logging.changeStatus(accountStatus)
25+
// this.logging.changeStatus(accountStatus)
2626
}
2727
}

0 commit comments

Comments
 (0)