无法在Angularjs

时间:2016-08-28 16:19:53

标签: asp.net angularjs asp.net-mvc

我无法使用ng-repeat(ng-repeat =“a student.store”)来工作。它不会抛出任何错误,但同时它不会渲染任何输出。请建议。

==============================

我的角色代码

var module = angular.module('MyApp', [])
                    .controller('MyController', function ($http, $scope) {
                        $http.get('/Department/getDept').success(function (response) {
                            $scope.departments =$scope.$eval(response);
                        });

                        $http.get('/Department/getStudents').success(function (resp) {
                            $scope.students = $scope.$eval(resp);
                        });

                        $scope.student = function(id)
                        {

                            var studentsInThatDepartment;
                            angular.forEach($scope.students, function (item) {
                                if (item.departmentId == id)
                                {
                                    studentsInThatDepartment = item;
                                }                            
                            })
                           $scope.store = angular.toJson(studentsInThatDepartment);
                            //return store;

                        }


=======================================================
mvc controller code
========================================================

public JsonResult getDept()
        {
            testdb_1Entities db = new testdb_1Entities();
            return new JsonResult
            {
                Data = JsonConvert.SerializeObject(db.tbl_departments.OrderBy(x => x.ID).ToList()),
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };

        }

        public JsonResult getStudents()
        {
            testdb_1Entities db = new testdb_1Entities();
            return new JsonResult
            {
                Data = JsonConvert.SerializeObject(db.tbl_students.OrderBy(x => x.id).ToList()),
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };

        }

==========================================
Index.cshtml code
==========================================

 <tbody>
                            <tr ng-repeat="a in student.store" ng-class-even="'even'" ng-class-odd="'odd'">
                                <td class="CX"><span>+</span></td>
                                <td>{{a.id}}</td>
                                <td>{{a.firstName}}</td>
                                <td>{{a.lastName}}</td>
                                <td>{{a.gender}}</td>
                                <td>{{a.departmentId}}</td>
                            </tr>

1 个答案:

答案 0 :(得分:0)

您指的是一个不存在的对象:您的代码中不存在student.store层次结构,因为您的数组(?)“存储”它直接分配给范围($scope.store)。

您可以做的是(除了清除您的代码):

对学生使用ng-repeat(<tr ng-repeat="a in students ...> </tr>)。可能这是您需要的集合(考虑到您对{{a.firstName}}等的使用)。

使用ng-repeat over store,但形式为<tr ng-repeat= "(key, value) in store" ...> </tr>(但那会很奇怪:为什么不使用{{store.myProperty}}呢?