0

我一直在尝试用 Angular 编写 todo mvc 应用程序,但我遇到了过滤问题。我要过滤的是已完成/活动或全部显示的待办事项。我有一个名为 isCompleted 的变量,我正在尝试对其进行过滤。奇怪的是,当我过滤以完成时,它似乎可以工作,但是当我尝试显示活动时,它同时显示活动和完成。我一直在尝试设置 data-ng-repeat="todo in todos | filter: {isComplete: false}" 但它仍然显示我所有的待办事项。关于我做错了什么的任何想法?

<section id="main" data-ng-cloak="">
<input type="checkbox" id="toggle-all" data-ng-model="toggleAll" data-ng-click="markAll(toggleAll)" />
<ul id="todo-list">
    <li data-ng-repeat="todo in todos | filter: selectFilter()" data-ng-class="{completed: todo.isCompleted}">
        <input type="checkbox" class="toggle" data-ng-model="todo.isCompleted" />
        <label>{{todo.title}}</label>
        <button class="destroy" data-ng-click="removeTodo(todo)"></button>
    </li>
</ul>

function TodosController($rootScope, $scope, $location) {

$scope.location = $location;
$scope.$watch('location.path()', function (path) {
    $scope.selectFilter = function () {
        if (path == '/active') {
            return { isCompleted: false };
        }
        else if (path == '/completed') {
            return { isCompleted: true };
        }
        else
        {
            return null;
        } 

    };
});

if (!$rootScope.todos) {
    $rootScope.todos = [{ title: "todo1", isCompleted: true }, { title: "todo3", isCompleted: true }, { title: "todo2", isCompleted: false }];
    //$rootScope.todos = [new Todo('Todo1', true), new Todo('Todo4', true), new Todo('Todo2', true), new Todo('Todo3', false)];
}
...
4

1 回答 1

2

<li>,添加

ng-show="toggleAll || todo.isCompleted"

这意味着当toggleAll被选中时,它会快捷方式isComplete并为每个项目返回true;如果toggleAll不勾选,则是否显示取决于isComplete.

Demo on jsFiddle

于 2013-08-02T13:52:17.597 回答