javascript - AngularJS ng:submit on 'enter' key - Stack Overflow

I'm trying to create a simple form that, upon hitting the 'enter' key, the entered data

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?

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?

Share Improve this question asked Jul 3, 2013 at 20:20 Shawn TaylorShawn Taylor 1,4645 gold badges26 silver badges36 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 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>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745653343a4638394.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信