Angular JS with localStorage Example

HTML

<div ng-app="toDo">
    <div ng-controller="AppCtrl">
        <form name="addContactForm">
            <label><b>Title:</b> <i>{{newTitle}}</i></label>
            <input ng-model="newTitle" type="text" required>
    
            <label><b>Full Name:</b> <i>{{newFullName}}</i></label>
            <input ng-model="newFullName" type="text" required>
    
            <label><b>Dollar Amount:</b> <i>{{newDollarAmount}}</i></label>
            <input ng-model="newDollarAmount" type="text" required>
    
            <label><b>Description:</b> <i>{{newDescription}}</i></label>
            <textarea ng-model="newDescription" required></textarea>
    
            <a ng-click="addContact()">&darr; Add to List</a>
            <p class="warning">{{failed}}</p>
        </form>
    
        <ul ng-repeat="contact in contacts" item="item">
            <li>Id: {{contact.id}}</li>
            <li>Title: {{contact.title}}</li>
            <li>Name: {{contact.fullName}}</li>
            <li>${{contact.dollarAmount}}</li>
            <li>{{contact.description}}</li>
            <button ng-click="deleteContact($index, contact)">Delete</button>
        </ul>
    </div>     
</div>

CSS

body{
			max-width: 800px;
			margin: auto;
			color: #555;
			font-size: 18px;
			font-family: sans-serif;
		}
		
		form,
		input, 
		textarea {
			display: block;
			width: 100%;
			margin: 20px auto;
			padding: 10px;
		}

		form {
			width: 60%
		}

		ul {
			padding: 20px;
			background-color: #ddd;
		}

		li {
			padding: 10px 0;
			margin-bottom: 10px; 
			border-bottom: 1px solid #ccc;
			list-style: none;
		}

		a:hover {			
			color: #3f83f4;
		}

		button:hover,
		a:hover {
			cursor: pointer;
		}

		.warning {
			color: #f76e74;
		}

JavaScript

var toDo = angular.module( 'toDo', [] );

toDo.controller('AppCtrl', ['$scope', function ($scope) {

    $scope.contacts = [];  // Array that will hold all contacts      
    $scope.failed = '';    // A message displayed if the form fails to submit

    // For each item in local storage...
    for( item in localStorage ) {

        // Parse the JSON string and add it to contacts array
        var newItem = JSON.parse( localStorage[item] );
        $scope.contacts.push( newItem );

    }

    // Submit new contact with values from the form fields, then reset values of the fields
    $scope.addContact = function() {   

        // Angular has special directives for forms, and a form name attribute allows that form to be accessed through the scope, which is where we get '$scope.addContactForm'
        // Read more here: docs.angularjs.org/api/ng/directive/form

        // If all required fields are complete 
        if( !$scope.addContactForm.$error.required ) { 

            // Remove warning
            $scope.failed = '';

            // Store contact data in an object         
            var newContact = {
                id: localStorage.length,    // id is used to identify this property when being delete from  storage  
                title: $scope.newTitle,
                fullName: $scope.newFullName,
                dollarAmount: $scope.newDollarAmount,
                description: $scope.newDescription
            };  

            // Add contact object to localStorage as the value to a new property
            localStorage.setItem( 'item' + localStorage.length, JSON.stringify(newContact) );

            // Add new contact object to the model by adding it to the contacts array
            $scope.contacts.push( newContact );

            // Reset the inputs values for the form
            $scope.newTitle = '';
            $scope.newFullName = '';
            $scope.newDollarAmount = '';
            $scope.newDescription = '';
        } else {
           ...