【问题标题】:not able to display json data in angularjs无法在angularjs中显示json数据
【发布时间】: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 方法中看到响应

标签: angularjs json crud


【解决方案1】:

尝试调试您的代码并在以下代码行设置断点,以确保 d.data 存在并将分配给 $scope.Contact:

$scope.Contact = d.data; // Success

另一件事是 responseResponse 似乎没有被定义。

我的最后一个建议是确保这些变量存在于当前控制器中。在 html 中的 div 上,您已经编写了 ng-controller="StudentController"。确保您的变量在该控制器中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-30
    • 1970-01-01
    • 2016-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多