【问题标题】:Angular template not displaying the service file data角度模板不显示服务文件数据
【发布时间】:2016-04-17 16:45:21
【问题描述】:

模板文件未显示服务文件中的数据,有人可以检查代码并纠正我。谢谢。

我还检查了控制器是否传递了数据,这正在发生。但是除了索引之外什么都没有显示。我以多种方式尝试了数据绑定,但问题似乎仍在重复。


html

<div class="panel3 panel-primary">
    <div class="panel-heading">Documents</div>
    <table class="table table-bordered table-condensed table-striped">

        <tr>
            <th>Serial Number</th>
            <th>Document Type</th>
            <th>Document Date</th>
            <th>Status</th>
            <th>Last Update Date</th>
        </tr>
        <tr ng-repeat="file in selectedDoc">
            <td>{{$index+1}}</td>
            <td>{{file.id.s.documentType}}</td>
            <td>{{file.id.s.documentDate}}</td>
            <td>{{file.id.s.status}}</td>
            <td>{{file.id.s.lastUpdateDate}}</td>
        </tr>

    </table>
</div>

控制器

"use strict";

angular.module("fleetDocumentsModule").controller("fleetDocumentsController",
    ['$scope', 'fleetDocumentsService',

        function ($scope, fleetDocumentsService) {


            $scope.selectedDoc = fleetDocumentsService.getDocuments();
            console.log("inside service file", $scope.selectedDoc)
        }]);

服务文件

"use strict";

angular.module("fleetDocumentsModule").service("fleetDocumentsService",

       function () {
           this.getDocuments = function () {
               return documents;
           };
           var documents = [
               {
                   "id": "1",
                   "s":[{                         
                           "documentType": 'POD',
                           "documentDate": '01-12-2015',
                           "status": 'Printed',
                           "lastUpdateDate": '28-12-2015'
                       },
                   {                       
                           "documentType": 'SIM',
                   "documentDate": '01-12-2015',
                   "status": 'Printed',
                   "lastUpdateDate": '28-12-2015'
               }                      
              ]},
                   {
                       "id": "2",                      
                       "s": [{
                            "documentType": 'DOC',
                            "documentDate": '01-12-2015',
                            "status": 'Printed',
                            "lastUpdateDate": '28-12-2015'
                        },
                    {
                        "documentType": 'LLTM',
                        "documentDate": '01-12-2015',
                        "status": 'Printed',
                        "lastUpdateDate": '28-12-2015'
                    }
                       ]},                   
                   {
                       "id": "3",                     
                       "s": [{
                           "documentType": 'TOM2',
                           "documentDate": '01-12-2015',
                           "status": 'Printed',
                           "lastUpdateDate": '28-12-2015'
                       },
                       {
                           "documentType": 'TOM3',
                           "documentDate": '01-12-2015',
                           "status": 'Printed',
                           "lastUpdateDate": '28-12-2015'
                       },
                   {
                       "documentType": 'TOM4',
                       "documentDate": '01-12-2015',
                       "status": 'Printed',
                       "lastUpdateDate": '28-12-2015'
                   }
                       ]},
           {
               "id": "4",
               "s":[{
                   "documentType": 'TTL',
                   "documentDate": '01-12-2015',
                   "status": 'Recieved',
                   "lastUpdateDate": '28-12-2015'
               }],
           },
           {
               "id": "5",
               "s":[{
               "documentType": 'RET',
               "documentDate": '01-12-2015',
               "status": 'Printed',
               "lastUpdateDate": '28-12-2015'
               }],
           }
           ]});

【问题讨论】:

    标签: angularjs angularjs-ng-repeat angularjs-service angularjs-controller angularjs-templates


    【解决方案1】:

    您没有在每个对象的视图中使用正确的属性

    属性file.s 是数组而不是对象并且没有file.id.s

    尝试改变:

    {{file.id.s.documentType}}
    

    {{file.s[0].documentType}}
    

    DEMO

    【讨论】:

    • 嗨,好的,它的调用,但它没有调用需要在“id”:“1”下显示的所有内容。例如:在 id:1 中,它必须显示所有 2 个对象。在 id:3 中,显示所有 3 个对象
    • 好的,如果您在浏览器中注意到,仅显示每个“s”数组中的第一组数据。如何显示该数组中的所有数据?
    • 建议您将数据映射到更扁平的数组...或 ng-repeat 上的 &lt;tbody&gt;ng-repeat 每个 s 的行示例:plnkr.co/edit/rnZvhPnocuU2HrrTvBl6
    【解决方案2】:

    您好,错误来自您引用对象的方式。你的对象结构是这样的:

    {
               "id": "4",
               "s":[{
                   "documentType": 'TTL',
                   "documentDate": '01-12-2015',
                   "status": 'Recieved',
                   "lastUpdateDate": '28-12-2015'
               }],
           }
    

    当您尝试以这种方式引用它时:

    <tr ng-repeat="file in selectedDoc">
            <td>{{$index+1}}</td>
            <td>{{file.id.s.documentType}}</td>
            <td>{{file.id.s.documentDate}}</td>
            <td>{{file.id.s.status}}</td>
            <td>{{file.id.s.lastUpdateDate}}</td>
        </tr>
    

    而不是这样:

    <tr ng-repeat="file in selectedDoc">
            <td>{{$index+1}}</td>
            <td>{{file.s[0].documentType}}</td>
            <td>{{file.s[0].documentDate}}</td>
            <td>{{file.s[0].status}}</td>
            <td>{{file.s[0].lastUpdateDate}}</td>
        </tr>
    

    您不需要 id 部分。 希望能解决!

    【讨论】:

    • property s 是数组,所以它没有对象属性
    • 谢谢。我只是注意到了这一点。还有你的答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-10
    • 2022-10-17
    • 2018-03-06
    • 1970-01-01
    • 2020-07-20
    相关资源
    最近更新 更多