【问题标题】:Pass value from angular to method将值从角度传递给方法
【发布时间】: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


【解决方案1】:
angular.module('ToDo', []).
    controller('todocontroller', [
        '$scope', "$http", function($scope, $http) {
            $scope.todos = [
                { 'title': 'build todo app', 'done': false }
            ];

            $scope.save = function (){
              $http({
                 method: 'POST', 
                 url: '/someUrl',
                 headers: {
                   "Content-Type": "application/json"
                 },
                 data: { todos: $scope.todos }
             });
           }


            $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;
                });
            }
        }
    ]);

在你的行动中

 [HttpPost]
 public ActionResult SaveListFromAngular(List<Todo> todos)
 {
     return View();
 }

 public class Todo 
 {
    public string Title { get; set; }
    public bool Done { get; set; }
 }

【讨论】:

  • 感谢您的回答,请查看我的编辑,由于某种原因,alert() 没有被命中?你能判断它看起来是否正确吗?
  • 非常感谢。 $http 有一个问题,它使用“隐式声明的全局变量 $http”。非常感谢您的宝贵时间。
  • 其实我忘记在控制器中添加 $http 依赖了。检查编辑。
  • 非常感谢!现在完美运行!
【解决方案2】:

我的建议是设置 WebApi 并将 Angular 与此 REST 服务层一起使用。

话虽如此,您需要做的就是发布如下字段:

  $http.post('api/Todo',{ /*object or list*/}).success(function(response) {
           console.log("success");
          }).error(function(err){
             console.log("failure")
          });

【讨论】:

  • 谢谢你,我相信你对 webApi 的推荐是一个很好的,我才刚刚开始,试图学习一些基础知识。当我感觉更舒服时,我会看看 WebApi =)
猜你喜欢
  • 2016-10-04
  • 1970-01-01
  • 1970-01-01
  • 2016-02-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-07
相关资源
最近更新 更多