I have this code that creates an todo-list in Angular.
<div class="row" ng-app="ToDo">
<div ng-controller="todocontroller">
<button ng-click="save()">Save</button>
<br />
<form name="frm" ng-submit="addTodo()">
<input type="text" name="newTodo" ng-model="newTodo" required />
<button ng-disabled="frm.$invalid">Go</button>
</form>
<button ng-click="clearCompleted()">ClearCompleted</button>
<ul>
<li id="test" ng-repeat="todo in todos">
<input type="checkbox" ng-model="todo.done"/>
<span ng-class="{'done':todo.done}">
{{todo.title}}
</span>
</li>
</ul>
</div>
</div>
The code works fine and creates a list of items as it should. I would now want to be able to pass the list to my method:
public ActionResult SaveListFromAngular()
{
//Do stuff
return View();
}
I dont know how to do this in Angular. Should I maybe iterate through the list somehow? Im interested in learning the correct way to do this in Angular which is a new framwork for me.
$scope.save = function () {
//Pass list to
//Home/SaveListFromAngular
}
Help appreciated! Thanks
EDIT: My complete scrip, suggestion added at the bottom:
angular.module('ToDo', []).
controller('todocontroller', [
'$scope', function($scope) {
$scope.todos = [
{ 'title': 'build todo app', 'done': false }
];
$scope.addTodo = function() {
$scope.todos.push({ 'title': $scope.newTodo, 'done': false });
$scope.newTodo = "";
}
$scope.clearCompleted = function() {
$scope.todos = $scope.todos.filter(function(item) {
return !item.done;
});
}
}
]);
function todocontroller($scope, $http) {
$scope.save = function () {
alert("fgfg");
$http({
method: 'POST',
url: '/Home/SaveListFromAngular',
headers: {
"Content-Type": "application/json"
},
data: { todos: $scope.todos }
});
}
}