Services as utilities

Apart from managing state and component interaction, services are also known for handling utility operations. Let's say that we want to start collecting new comments in our comment app. We don't know much about forms yet, so we can use the browser's prompt. We expect the user to pass both the username and the content via the same textbox in the prompt, as follows:

<username>: <comment content>

Therefore, we need a utility method to extract these bits from the textbox into a comment object with the author and content properties. Let's start with collecting the information from the comment list component:

showPrompt() { const commentString = window.prompt('Please enter your username and content: ', 'username: content'); ...

Get TypeScript 2.x for Angular Developers now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.