Skip to main content
 首页 » 编程设计

javascript之将 AngularJS 与 ASP.NET MVC ng-repeat 一起使用在表中为空

2024年02月05日24www_RR

我有一个 HTML 表格如下:

   <div class="form-group" ng-controller="GetStudentController"> 
            <table class="table table-responsive table-bordered table-hover form-group"> 
                <thead> 
                    <tr> 
                        <td> 
                            Student ID 
                        </td> 
                        <td> 
                            Ime 
                        </td> 
                        <td> 
                            Prezime 
                        </td> 
                    </tr> 
                </thead> 
                <tbody> 
                    <tr data-ng-repeat="s in data track by $index"> 
                        <td> 
                            {{s.StudentID}} 
                        </td> 
                        <td> 
                            {{s.Ime}} 
                        </td> 
                        <td> 
                            {{s.Prezime}} 
                        </td> 
                    </tr> 
                </tbody> 
            </table> 
        </div> 

而且我已经为我的实验设置了小 Angular 模块/服务和 Controller 。作为后端服务,我使用 C# 作为前端服务,我使用 AngularJS 来获取我的数据并将其显示在 View 中,如下所示:

var app = angular.module("myApp", []); 
 
app.service("GetStudentsService", function ($http) { 
    this.getData = function () 
    { 
        return $http({ 
            metod: "GET", 
            url: "/Home/GetStudent" 
        }).success(function (data) { 
            return data; 
        }).error(function () { 
            alert("error"); 
            return null; 
        }); 
    } 
}); 
app.controller("GetStudentController", function ($scope, GetStudentsService) { 
    $scope.data = null; 
    GetStudentsService.getData().then(function (response) { 
        $scope.data = response; 
    }); 
}); 

这是我的服务:

  public JsonResult GetStudent() 
        { 
            return new JsonResult { Data = Connection.dc.Students.ToList(), JsonRequestBehavior = JsonRequestBehavior.AllowGet }; 
        } 

Angular 可以正常获取数据,但它没有将其绑定(bind)在 {{}} 括号内......我已经在控制台中检查了错误迹象,但似乎没有任何错误......表格显示如下:

有人知道为什么会这样吗? :/

请您参考如下方法:

尝试跟随

$scope.data = response.data;