改变角度待办事项列表中的颜色

时间:2017-03-18 21:02:05

标签: javascript html css angularjs

嗨,这是我第一次来这里,所以请耐心等待我发现问题 我需要更改span类中的颜色这是我的代码

当我点击从某种颜色到灰色的复选框时,点击事件中的颜色需要更改

如果有人想帮助我尝试用选定的选项进行选择,但角度

tnx for the helper。

function TodoCtrl($scope) {
  $scope.priority = ['Urgent', 'Critical', 'Normal', 'IfYouCan']

  $scope.todos = [{
    text: 'Attend Selection Day',
    done: false,
    lvl: 'Critical'
  }, {
    text: 'Register to Full Stack Web Course',
    done: false,
    lvl: 'Normal'
  }, {
    text: 'Go see X-Man apocalypse movie',
    done: false,
    lvl: 'IfYouCan'
  }];

  $scope.addTodo = function() {

    $scope.todos.push({
      text: $scope.todoText,
      done: false,
      lvl: $scope.todoLvl
    });

    $scope.todoText = '';
  };

  $scope.remaining = function() {
    var count = 0;

    angular.forEach($scope.todos, function(todo) {
      count += todo.done ? 0 : 1;
    });


    return count;
  };

  $scope.archive = function() {
    var oldTodos = $scope.todos;

    $scope.todos = [];

    angular.forEach(oldTodos, function(todo) {
      if (!todo.done) $scope.todos.push(todo);
    });
  };
}
.todo-true {
  text-decoration: line-through;
  color: grey;
}

.todo-Urgent {
  color: red;
}

.todo-Critical {
  color: orange;
}

.todo-Normal {
  color: green;
}

.todo-IfYouCan {
  color: RoyalBlue;
}

div.frame {
  position: absolute;
  margin: 10px 0px 0px 50px;
}

div.todo {
  height: 20px;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.11/angular.min.js"></script>
<div ng-app>
  <div class="frame">
    <h2>Todo</h2>
    <div ng-controller="TodoCtrl">

      <div>
        <input type="text" ng-model="todoText" size="30" placeholder="add new todo here">
        <select ng-init="todoLvl = priority[2]" ng-model="todoLvl">
          <option ng-repeat="item in priority" value="{{item}}">{{item}}</option>
        </select>
        <button ng-click="addTodo()" type="button">add</button>
      </div>
      <hr />
      <span>{{remaining()}} of {{todos.length}} remaining</span>
      <br />
      <div ng-repeat="todo in todos">
        <input type="checkbox" ng-model="todo.done">
        <span class="done-{{todo.done}}"><span class="todo-{{todo.lvl}}">{{todo.text}} {{todo.lvl}}</span></span>
      </div>
      <br />
      <a href="" ng-click="archive()">Completed</a>
    </div>
  </div>
</div>

1 个答案:

答案 0 :(得分:1)

您可以添加&#34; ng-class&#34;基于你的todo.done(ng-model)。

还为颜色添加更具体的选择器。

&#13;
&#13;
function TodoCtrl($scope) {
  $scope.priority = ['Urgent', 'Critical', 'Normal', 'IfYouCan']

  $scope.todos = [{
    text: 'Attend Selection Day',
    done: false,
    lvl: 'Critical'
  }, {
    text: 'Register to Full Stack Web Course',
    done: false,
    lvl: 'Normal'
  }, {
    text: 'Go see X-Man apocalypse movie',
    done: false,
    lvl: 'IfYouCan'
  }];

  $scope.addTodo = function() {

    $scope.todos.push({
      text: $scope.todoText,
      done: false,
      lvl: $scope.todoLvl
    });

    $scope.todoText = '';
  };

  $scope.remaining = function() {
    var count = 0;

    angular.forEach($scope.todos, function(todo) {
      count += todo.done ? 0 : 1;
    });


    return count;
  };

  $scope.archive = function() {
    var oldTodos = $scope.todos;

    $scope.todos = [];

    angular.forEach(oldTodos, function(todo) {
      if (!todo.done) $scope.todos.push(todo);
    });
  };
}
&#13;
span.todo-true {
  text-decoration: line-through;
  color: grey;
}

.todo-Urgent {
  color: red;
}

.todo-Critical {
  color: orange;
}

.todo-Normal {
  color: green;
}

.todo-IfYouCan {
  color: RoyalBlue;
}

div.frame {
  position: absolute;
  margin: 10px 0px 0px 50px;
}

div.todo {
  height: 20px;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.11/angular.min.js"></script>
<div ng-app>
  <div class="frame">
    <h2>Todo</h2>
    <div ng-controller="TodoCtrl">

      <div>
        <input type="text" ng-model="todoText" size="30" placeholder="add new todo here">
        <select ng-init="todoLvl = priority[2]" ng-model="todoLvl">
          <option ng-repeat="item in priority" value="{{item}}">{{item}}</option>
        </select>
        <button ng-click="addTodo()" type="button">add</button>
      </div>
      <hr />
      <span>{{remaining()}} of {{todos.length}} remaining</span>
      <br />
      <div ng-repeat="todo in todos">
        <input type="checkbox" ng-model="todo.done">
        <span class="done-{{todo.done}}"><span class="todo-{{todo.lvl}}" ng-class="{'todo-true': todo.done}">{{todo.text}} {{todo.lvl}}</span></span>
      </div>
      <br />
      <a href="" ng-click="archive()">Completed</a>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;