Angularjs 使用ng repeat和factory无法显示来自控制器的数据
我正在尝试使用ng repeat将数据显示在表中 我的工厂编码如下:Angularjs 使用ng repeat和factory无法显示来自控制器的数据,angularjs,Angularjs,我正在尝试使用ng repeat将数据显示在表中 我的工厂编码如下: /// <reference path="../angular.js" /> var studentService = angular.module('StudentService', []); studentService.factory('studentApi', function ($http) { var urlBase = "http://localhost:60892/api";
/// <reference path="../angular.js" />
var studentService = angular.module('StudentService', []);
studentService.factory('studentApi', function ($http) {
var urlBase = "http://localhost:60892/api";
var student = {};
student.getStudents = function() {
return $http.get(urlBase + '/Students');
}
return student;
})
但当我试图在html表中打印它时,该表创建了6行,但其中没有任何值。即使使用inspect元素查看
值,也没有。下面是我的html代码
<div class="row" >
<h1 class="text-center text-primary">Students</h1>
<hr/>
<div class="row">
<div class="col-md-9 col-md-offset-1">
<input type="text" placeholder="Type to filter records" class="input-lg form-control ng-model="txtFilter"/>
<br/>
<br/>
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>Student Id</th>
<th>First Name</th>
<th>Last Name</th>
<th>Age</th>
<th>Gender</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="s in students track by $index">
<td>{{s.Id}}</td>
<td>{{s.FirstName}}</td>
<td>{{s.LastName}}</td>
<td>{{s.Age}}</td>
<td>{{s.Gender}}</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
学生
您正在将从web服务获得的response
对象分配给$scope.students
。由于此原因,视图未得到更新。响应
对象不是数组,因此它不会迭代。响应对象包含数据
字段,该字段将保存来自服务器的数据。所以您需要从响应对象分配数据字段
只需在控制器的getStudents()
方法中做一个更改
$scope.students = students;
到
我认为您需要这个$scope.students=students.data;而不是$scope.students=students;当你打电话给你的service@stackquestions很乐意帮忙。
<div class="row" >
<h1 class="text-center text-primary">Students</h1>
<hr/>
<div class="row">
<div class="col-md-9 col-md-offset-1">
<input type="text" placeholder="Type to filter records" class="input-lg form-control ng-model="txtFilter"/>
<br/>
<br/>
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>Student Id</th>
<th>First Name</th>
<th>Last Name</th>
<th>Age</th>
<th>Gender</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="s in students track by $index">
<td>{{s.Id}}</td>
<td>{{s.FirstName}}</td>
<td>{{s.LastName}}</td>
<td>{{s.Age}}</td>
<td>{{s.Gender}}</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
$scope.students = students;
$scope.students = students.data;