TodoList exercises for angularjs beginners

Source: Internet
Author: User
Tags tojson

TodoList exercises for angularjs beginners

At the beginning, I learned how to use the todo exercises to preliminarily understand the powerful functions of the ng framework and record several problems encountered in the process:

1. Use localStorage to store data. angular. toJson is used for serialization, because ng-repeat will add the $ hashkey attribute to the array object, and JSON. stringify will not be used for serialization.

2. The exercises use ng of version 1.4, and many examples on the Internet are earlier versions. The syntax has changed a lot and I have been pondering for a long time.

 

 

REFERENCE The bootstrap3 style file ,:

 

HTML:

 

     My task list Content Time Incomplete Completed All 
  • {Task. text }}×{Task. time }}
Unfinished: {count ()}, completed: {countDone ()}, total: {taskList. length }}< script src = libs/angular. js> </script> <script src = index. js> </script>
JS:

 

 

Angular. module ('tasklist', []). controller ('taskcontroller', function ($ scope, dateFilter) {var tmp = localStorage. getItem ('tasklist'); $ scope. taskList = tmp? Angular. fromJson (tmp): []; // The statistics are incomplete $ scope. count = function () {var count = 0; angular. forEach ($ scope. taskList, function (task) {count + = task. done? 0: 1 ;}); return count ;}; // The statistics have been completed $ scope. countDone = function () {var count = 0; angular. forEach ($ scope. taskList, function (task) {count + = task. done? 1: 0 ;}); return count ;}; // mark all complete $ scope. allDone = function () {angular. forEach ($ scope. taskList, function (task) {task. done = $ scope. isAllDone;}); $ scope. save () ;}$ scope. hasTask = function () {return $ scope. taskList. length> 0;} // Add $ scope. addTask = function () {$ scope. taskList. push ({id: $ scope. taskList. length + 1, text: $ scope. taskText, done: false, time: getNowTime ()}); $ scope. taskText = ''; $ scope. save () ;}; // Delete $ scope. removeTask = function (todo) {$ scope. taskList. splice ($ scope. taskList. indexOf (todo), 1); $ scope. save () ;}; // save $ scope. save = function () {// angular is used for serialization. toJson, because ng-repeat will add the $ hashkey attribute inside the object, using JSON. stringify serialization does not filter localStorage. setItem ('tasklist', angular. toJson ($ scope. taskList);} function getNowTime () {return dateFilter (new Date (), 'yyyy-MM-dd HH: mm: ss ');}});


 

CSS:

 

body {padding: 20px 0;background-color: #f5f5f5;}.container{    width:600px;    padding:20px 30px;    background-color:white;    border-radius:5px;    border: 1px solid #e5e5e5;}.taskList{    padding:0;    margin-top:20px;}.taskList li{    list-style:none;    border-bottom:1px solid #ddd;}.done-true{    text-decoration:line-through;}.close{    margin-left:10px;}

 

 



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.