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;}