1

我是整体编程的新手,这是我第一次尝试使用 Angular,所以请在回答时考虑到这一点。有人建议我尝试使用 Angular 构建一个待办事项列表以了解有关 java 脚本的更多信息,但当单击“添加任务”按钮时,我被困在如何让我的数据(待办事项)发布到我的表单上。表单会重置,所以我知道按钮正在“工作”,但我的项目没有呈现。如果有人可以帮助我,我将不胜感激。

以下是我的代码的相关部分。预先感谢您的帮助。

html:

   <form ng-submit= "addTodo()" class= "list-group-item">
     <input type= "text" placeholder= "Add your task here" ng-model= "newTodo"/>
      <select ng-model= "todoPriority">
       <option value= "normal">Low</option>
       <option value= "normal">Medium</option>
       <option value= "normal">High</option>
     </select>
    <button ng-click= "addTodo()">Add Task to List</button>
  </form>

应用程序.js

        (function(){
  var app = angular.module('Todo-app', []);
    app.controller('TodoCtrl', function($scope){
        $scope.todos= [];

        $scope.todoPriority= 'normal';

        $scope.addTodo= function(){
          var newTodo={
            done: false,
            text: $scope.todoText,
            priority: $scope.todoPriority
          };
            $scope.todos.push(newTodo);
            $scope.todoText = '';
            $scope.todoPriority = 'normal';
        };


        $scope.getTotal= function(){
      return $scope.todos.length;
    };

   $scope.removeTodo= function(start){

      $scope.todos.splice(start, 1);
    };

    $scope.move= function(index, direction){
      if (direction === 'up'){
        if (index === 0){
          return;
          }
          index = index -1;
        }
      if (direction === 'down'){
         if (index === $scope.todos.length -1) {
         return;
         }
      }
    var todo= $scope.todos[index];
    $scope.todos.splice(index+2,0, todo);
    $scope.todos.splice(index, 1);
    };
 });

});
4

0 回答 0