0

I've followed this basic AngularJS tutorial : https://www.youtube.com/watch?v=WuiHuZq_cg4&list=PL173F1A311439C05D

Everything went well until I added the clearCompleted() method. it does not seem to be working:

HTML:

<!DOCTYPE html>
<html ng-app>
<head>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js"></script>
    <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.6.0/underscore-min.js"></script>
    <script type="text/javascript" src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="todo.js"></script>
    <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
    <link rel="stylesheet" href="todo.css">
</head> 
<body>
    <div ng-controller="TodoCtrl">

        <h2>Total todos: {{getTotalTodos()}} </h2>

        <ul class="unstyled">
            <li ng-repeat="todo in todos">

                <input type="checkbox" ng-model="todo.done">
                <span class="done-{{todo.done}}">{{todo.text}}</span>

            </li>
        </ul>

        <form class="form-horizontal">
            <input type="text" ng-model="formTodoText" ng-model-instant>
            <button class="btn" ng-click="addTodo()"><i class="icon-plus"></i>Add</button>

        </form>

        <button class="btn-large" ng-click="clearCompletedTodos()">

        Clear Completed

        </button>
    </div>

</body>

</html>

JS:

function TodoCtrl($scope) {

    $scope.todos = [
        {text: 'Learn Anagular', done:false}, 
        {text: 'Build an app', done:false}
    ];

    $scope.getTotalTodos = function() {

        return $scope.todos.length;

    };

    $scope.addTodo = function() {

        $scope.todos.push({text: $scope.formTodoText, done: false});
        $scope.formTodoText = '';
    };

    $scope.clearCompletedTodos = function() {

        $scope.todos = _.filter($scope.todos, function(todo) {

                return !todo.done;

        });

    };
}

the "completed todos" are not getting removed .

10
  • yes but its !todo.done , i.e. not done. Commented Mar 27, 2014 at 14:31
  • I'd suggest using a tutorial that is using a more recent version of Angular. 1.0.1 is fairly ancient now. Commented Mar 27, 2014 at 14:32
  • I think its the _.filter() thats the problem, not sure why though. Commented Mar 27, 2014 at 14:32
  • @DavinTryon yes I will do that, but this should still work returning not completed todos is pretty simple. Commented Mar 27, 2014 at 14:33
  • the docs show $filter('filter') but that doesnt fix it either. Commented Mar 27, 2014 at 14:34

3 Answers 3

1

Here is what I have done and it works for me:

Please notice that the follow lines has been changed (http:// added)

 <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.6.0/underscore-min.js"></script>
<script type="text/javascript" src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
<script type="text/javascript" src="todo.js"></script>
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">

to the follow

 <script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.6.0/underscore-min.js"></script>
<script type="text/javascript" src="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
<script type="text/javascript" src="todo.js"></script>
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
Sign up to request clarification or add additional context in comments.

Comments

0

I met the same problem, actually, write it like below, it works fine:

$scope.clearCompleted = function(){
    $scope.todos = $scope.todos.filter(function(todo){
        return !todo.done;
    })
};

Comments

0

Actually none of those worked for me; I used this one.

var t = [];
angular.forEach($scope.todos, function (i)
{
    if (!i.done)
        t.push({text: i.text, done: i.done});
});
$scope.todos = t;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.