A TODO app that leverages HTML5 localstorage functionality (ANGULARJS+BOOTSTRAP)

Source: Internet
Author: User

It's interesting to see a simple Todo app on the web today, using ANGULARJS to do front-end data binding and using Localstorage to store data.

<!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd "><HTMLNg-app= "Todoapp">  <Head><Metahttp-equiv= "Content-type"content= "text/html; charset=iso-8859-1"/><Scriptsrc= "/js/angular.js"type= "Text/javascript"></Script><Scriptsrc= "//netdna.bootstrapcdn.com/bootstrap/3.0.3/js/bootstrap.min.js"type= "Text/javascript"></Script><Linkrel= "stylesheet"href= "//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css"type= "Text/css"/><Scripttype= "Text/javascript"src= "/less/js/app.js"></Script><style>. Center-grey{background:#f2f2f2;Margin-top: -;  }. Top-buffer{Margin-top:20px;   }Button{Display:Block;width:100%;  }</style><title>Angular Todo Note App</title>  </Head>  <Body><Divclass= "Container Center-grey"Ng-controller= "Todocontroller">  <Divclass= "Row Top-buffer" ><spanclass= "Col-md-3"></span><spanclass= "Col-md-5">  <inputclass= "Form-control"type= "text"Ng-model= "Note"placeholder= "Add a note here"/> </span><spanclass= "Col-md-1">  <ButtonNg-click= "Createnote ()"class= "Btn btn-success">Add</Button></span><spanclass= "Col-md-3"></span>  </Div>  <Divclass= "Row Top-buffer" ><spanclass= "Col-md-3"></span><spanclass= "Col-md-6">  <ulclass= "List-group"><binng-repeat= "Note in Notes track by $index"class= "List-group-item">  <span>{{Note}}</span></Li>  </ul></span><spanclass= "Col-md-3"></span>  </Div></Div>  </Body></HTML>

Ng-app declares the use of Todoapp as model

Ng-controller declares Todocontroller as the controller

Ng-model= "Note" Data is bound to the input box, which is bound to the $scope.note

ng-click= "Createnote" binds a function (object), bound to $scope.createnote = function () {...}

Ng-repeat = "Note in Notes track by $index" and {{Note}} does not look familiar, right, is the ANGULARJS loop traversal, listing all the notes array of data

The following is Todoapp JS Kurding, controller ' Todocontroller ' incoming $scope and notesfactory services,

Object.keys Traverse all the Key-value key pairs in the Localstorage

varTodoapp = Angular.module (' Todoapp '), []); Todoapp.controller (' Todocontroller ',function($scope, notesfactory) {$scope. Notes=Notesfactory.get (); $scope. Createnote=function() {notesfactory.put ($scope. Note); $scope. Note= ' '; $scope. Notes=Notesfactory.get (); }}); Todoapp.factory (' Notesfactory ',function(){    return{put:function(note) {Localstorage.setitem (' Todo ' + (Object.keys (localstorage). length + 1), note); }, get:function(){        varNotes = []; varKeys =Object.keys (localstorage);  for(vari = 0; i < keys.length; i++) {Notes.push (Localstorage.getitem (keys[i)); }        returnnotes; }           };})

BTW, through the Firefox developer Tool can see the changes to delete the current page of localstorage content, specific methods please poke here

The last thing I want to say is that Angularjs is really a good stuff.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.