0

I'm trying to create a simple form that, upon hitting the 'enter' key, the entered data will be sent and appended to a list in my controller. The code I have is as follows:

controller.js

.controller('GenericTodosCtrl', function($scope) {

    $scope.todos=[];
    $scope.newTodo="";

    function addTodo() {
        alert("hello");
        $scope.todos.push({
            content: $scope.newTodo,
            done: false,
            editing: false
        });
        $scope.newTodo = "";
    };
});

main.html

<form ng:submit="addTodo()">
    <input name="newTodo" placeholder="Placeholder" type="text">
</form>

Currently, when I type in values and hit 'enter' nothing happens (not even the alert). I can't seem to figure out if I should be using "this" instead of $scope for things in controller.js. Does ng:submit require something in the ? Or is this an issue with my javascript instead?

1 Answer 1

4

You should make addTodo part of your $scope.

$scope.addTodo = function() {
    alert("hello");
    $scope.todos.push({
        content: $scope.newTodo,
        done: false,
        editing: false
    });
    $scope.newTodo = "";
};

Also, make sure the value in the input field is actually databound to your $scope value by using ngModel:

<form ng:submit="addTodo()">
    <input ng:model="newTodo" placeholder="Placeholder" type="text">
</form>
Sign up to request clarification or add additional context in comments.

2 Comments

That's perfect. Just starting out with things so thanks for the clear and concise response.
You're welcome. Enjoy learning AngularJS, it's fantastic stuff.

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.