【发布时间】:2014-08-11 06:35:18
【问题描述】:
我有这段代码可以在 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>
代码可以正常工作,并按应有的方式创建项目列表。我现在希望能够将列表传递给我的方法:
public ActionResult SaveListFromAngular()
{
//Do stuff
return View();
}
我不知道如何在 Angular 中做到这一点。我应该以某种方式遍历列表吗? 我有兴趣在 Angular 中学习正确的方法,这对我来说是一个新框架。
$scope.save = function () {
//Pass list to
//Home/SaveListFromAngular
}
帮助表示赞赏! 谢谢
编辑: 我的完整脚本,在底部添加了建议:
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 }
});
}
}
【问题讨论】:
标签: angularjs