一個利用HTML5 localStorage功能的todo應用(angularJs+Bootstrap)

來源:互聯網
上載者:User

標籤:style   blog   http   color   java   使用   os   io   

今天在網上看到一個簡單的todo應用,使用angularJs做前端資料繫結,利用localStorage來儲存資料,覺得挺有意思的。

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html ng-app="todoApp">  <head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/><script src="/js/angular.js" type="text/javascript"></script><script src="//netdna.bootstrapcdn.com/bootstrap/3.0.3/js/bootstrap.min.js" type="text/javascript"></script><link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css" type="text/css"/><script type="text/javascript" src="/less/js/app.js"></script><style>  .center-grey{ background:#f2f2f2; margin-top:20;  }  .top-buffer {  margin-top:20px;   }  button{  display: block;   width: 100%;  }</style><title>Angular Todo Note App</title>  </head>  <body><div class="container center-grey" ng-controller="TodoController">  <div class="row top-buffer" ><span class="col-md-3"></span><span class="col-md-5">  <input class="form-control" type="text" ng-model="note" placeholder="Add a note here"/> </span><span class="col-md-1">  <button ng-click="createNote()" class="btn btn-success">Add</button></span><span class="col-md-3"></span>  </div>  <div class="row top-buffer" ><span class="col-md-3"></span><span class="col-md-6">  <ul class="list-group"><li ng-repeat="note in notes track by $index" class="list-group-item">  <span>{{note}}</span></li>  </ul></span><span class="col-md-3"></span>  </div></div>  </body></html>

ng-app聲明了使用todoApp作為model

ng-controller聲明了TodoController作為控制器

ng-model="note" 對輸入框input進行了資料繫結,綁定到了$scope.note

ng-click="createNote"對函數(對象)進行了綁定,綁定到了$scope.createNote = function(){...}

ng-repeat = "note in notes track by $index" 以及{{note}} 是不是看上去很眼熟,對了,就是angularJs的迴圈遍曆的寫法,列出所有的notes數組裡的資料

下面就是todoApp的js寇丁,controller‘TodoController‘傳入$scope和notesFactory服務,

object.keys遍曆localStorage裡所有的key-value索引值對

var todoApp = 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(){        var notes = [];        var keys = Object.keys(localStorage);        for(var i = 0; i < keys.length; i++){            notes.push(localStorage.getItem(keys[i]));        }        return notes;    }           };})

BTW,通過firefox的開發人員工具可以查看修改刪除當前頁面的localStorage內容,具體方法請猛戳這裡

最後想說的是,angularJS真是一個好東西啊

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.