0
0
Angularframework~10 mins

Service-based state management in Angular - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to inject the service into the component constructor.

Angular
constructor(private [1]: CounterService) {}
Drag options to blanks, or click blank then click option'
AserviceCounter
Bcounter
CCounterService
DcounterService
Attempts:
3 left
💡 Hint
Common Mistakes
Using the service class name directly instead of a variable name.
Using unrelated variable names.
2fill in blank
medium

Complete the code to call the increment method from the service on button click.

Angular
<button (click)="[1].increment()">Increase</button>
Drag options to blanks, or click blank then click option'
Aincrement
BCounterService
CcounterService
Dservice
Attempts:
3 left
💡 Hint
Common Mistakes
Using the service class name instead of the instance variable.
Calling the method directly without the service instance.
3fill in blank
hard

Fix the error in the service method to update the count correctly.

Angular
increment() {
  this.count [1] 1;
}
Drag options to blanks, or click blank then click option'
A+=
B-=
C==
D=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' which assigns but does not add.
Using '==' which is a comparison operator.
4fill in blank
hard

Fill both blanks to create a getter that returns the current count from the service.

Angular
get currentCount() {
  return this.[1].[2];
}
Drag options to blanks, or click blank then click option'
AcounterService
Bcount
Cservice
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Using the service class name instead of the instance variable.
Using incorrect property names.
5fill in blank
hard

Fill all three blanks to create a service method that resets count to zero and emits the new value.

Angular
reset() {
  this.[1] = [2];
  this.[3].next(this.count);
}
Drag options to blanks, or click blank then click option'
Acount
B0
CcountChanged
DresetCount
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong property names for count or the subject.
Assigning count to a string instead of zero.