gpt4 book ai didi

javascript - AngularJS 过滤

转载 作者:行者123 更新时间:2023-12-02 18:29:03 25 4
gpt4 key购买 nike

我一直在尝试用 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)];
}
...

最佳答案

<li> ,添加

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

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

Demo on jsFiddle

关于javascript - AngularJS 过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18016770/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com