【发布时间】:2018-07-27 14:33:23
【问题描述】:
我正在尝试学习 AngularJs。 所以我正在处理这个示例,我需要在此处输入代码以在 html 上显示数据库记录。
但我得到的只是
Name {{Response.Name}}
Father Name {{response.FatherName}}
Address {{Contact.Address}}
Phone Number {{Contact.PhoneNumber}}
我没有得到值。
这是我的 fetchdata,js
var MyApp = angular.module('FetchData', []);
MyApp.controller('', function ($scope, RecordService) {
//inject $scope and RecordService.
$scope.Contact = null;
RecordService.GetRecord().then(function (d) {
$scope.Contact = d.data; // Success
}, function () {
alert('Failed'); // Failed
});
});
MyApp.factory('RecordService', function ($http) {
var fac = {};
//GetRecord function will call the GetStudentRecord action method.
fac.GetRecord = function () {
return $http.get('/StudentController/GetStudentRecord');
}
return fac;
});
好的,这是我的 html
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml"; //SHared layout
}
<h2>How to Fetch Data From Database using AngularJs.</h2>
<div ng-controller="StudentController">
<table class="table table-responsive caption">
<tr>
<td>Name </td>
<td>{{Response.Name}}</td>
</tr>
<tr>
<td>Father Name </td>
<td>{{response.FatherName}}</td>
</tr>
<tr>
<td>Address</td>
<td>{{Contact.Address}}</td>
</tr>
<tr>
<td>Phone Number</td>
<td>{{Contact.PhoneNumber}}</td>
</tr>
</table>
</div>
【问题讨论】:
-
什么不起作用
-
请您尝试添加一些其他信息吗?像控制台日志
-
值没有显示在 cshtml 索引视图上
-
但是您确定其余服务的调用顺利吗? @user3500179 我看到控制器声明有点错误。
MyApp.controller('', function ($scope, RecordService)必须是MyApp.controller('$scope', 'RecordService', function ($scope, RecordService)但我想确保休息电话顺利。 -
是的,我可以在 getStudent 方法中看到响应