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 badges1 Answer
Reset to default 4You 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条)