Implementing Local Storage Persistence in an Angular Application
This challenge focuses on adding persistence to an Angular application using local storage. The goal is to allow a simple list of items to be saved locally and restored upon application reload, providing a basic form of data persistence. This is a common requirement in many applications to improve user experience and data retention.
Problem Description
You are tasked with creating an Angular component that manages a list of strings. This component should allow users to add new strings to the list and display the current list. Crucially, the list should be persisted in the browser's local storage. When the application reloads, the list should be restored from local storage.
What needs to be achieved:
- Create an Angular component with a list of strings.
- Implement a function to add new strings to the list.
- Implement a function to save the list to local storage.
- Implement a function to load the list from local storage when the component initializes.
- Display the list in the component's template.
Key Requirements:
- The list should be stored in local storage under the key "myAngularList".
- The list should be serialized as a JSON string when saved to local storage and parsed back into an array of strings when loaded.
- If no data exists in local storage under the specified key, an empty array should be used as the initial list.
- The component should handle potential errors during local storage access gracefully (though explicit error handling beyond checking for null/undefined is not required for this challenge).
Expected Behavior:
- On initial load, if "myAngularList" exists in local storage, the component should display the list stored there.
- If "myAngularList" does not exist, the component should display an empty list.
- Adding a new string to the list should update the displayed list and save the updated list to local storage.
- Reloading the application should display the last saved list from local storage.
Edge Cases to Consider:
- Local storage might be disabled in the user's browser. (While you don't need to explicitly handle this, be aware it's a possibility).
- The local storage key might contain unexpected data (though the JSON parsing should handle this reasonably).
- Very large lists might impact performance (though this is not a primary concern for this challenge).
Examples
Example 1:
Input: Initial local storage is empty. User adds "Apple", "Banana", and "Cherry".
Output: The component displays: ["Apple", "Banana", "Cherry"]. Local storage contains: '["Apple", "Banana", "Cherry"]'
Explanation: The list is initialized as empty, items are added, and each addition saves the updated list to local storage.
Example 2:
Input: Local storage contains: '["Orange", "Grape"]'. User adds "Kiwi".
Output: The component displays: ["Orange", "Grape", "Kiwi"]. Local storage contains: '["Orange", "Grape", "Kiwi"]'
Explanation: The list is loaded from local storage, an item is added, and the updated list is saved.
Example 3:
Input: Local storage contains: '["Pear", "Mango"]'. User reloads the page.
Output: The component displays: ["Pear", "Mango"].
Explanation: The list is loaded from local storage upon component initialization.
Constraints
- The list will contain only strings.
- The component should be implemented using standard Angular practices (no external libraries beyond Angular itself).
- Performance is not a primary concern; focus on correctness and clarity.
- The component should be self-contained and easily testable.
Notes
- Consider using Angular's
OnInitlifecycle hook to load the data from local storage when the component is initialized. - Use
localStorage.setItem()andlocalStorage.getItem()to interact with local storage. - Remember to serialize the list to JSON before saving it and parse it back from JSON when loading it.
- Think about how to structure your component's code for readability and maintainability. A separate service for local storage interaction could be beneficial, but is not required for this challenge.