2017-05-25 77 views
1

该表格应填入从数组中取得的数据。之前的代码已经在其他应用程序上工作过,所以我复制了该脚本,并且出于某种原因数据未加载。控制台中没有显示错误。错误必须存在于未找到的数据中。任何想法为什么?ng-repeat不填充表格数据

<tr ng-repeat="x in patents"> 
    <td>{{x.id}} h</td> 
    <td>{{x.applicationnumber}}</td> 
    <td>{{x.clientref}}</td> 
    <td>$ {{x.costtorenew}}</td> 
    <td>{{x.renewalduedate}}</td> 
    <td><button type="button" class="btn btn-danger" ng-click="remove(x.id)">Remove</button></td> 
</tr> 

var app = angular.module('myApp', ['ngRoute', 'angularMoment']); 

app.config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider){ 
    $routeProvider 
    .when('/list-patents', { 
     templateUrl: '/templates/list-patents.htm', 
     controller: 'patentCtrl' 
    }) 

app.controller('patentCtrl', ['$scope', '$http', 'loadPatents', function($scope, $http, loadPatents) { 

var self = this; 
self.patent={id:null,applicationnumber:'',clientref:'',costtorenew:'',renewalduedate:'',basketstatus:''}; 
self.patents=[]; 
self.remove = remove; 

fetchAllPatents(); 

function fetchAllPatents(){ 
    loadPatents.fetchAllPatents() 
     .then(
     function(d) { 
      self.patents = d; 
     }, 
     function(errResponse){ 
      console.error('Error while fetching Users'); 
     } 
    ); 
    console.log(self.patents) 
} 

}]); 

app.factory('loadPatents', function($http, $q) { 

    var factory = {}; 

    var REST_SERVICE_URI = 'http://localhost:8080/Sprint002b/restpatent/'; 

    factory.fetchAllPatents = function() { 

     var deferred = $q.defer(); 
     $http.get(REST_SERVICE_URI) 
      .then(
      function (response) { 
       deferred.resolve(response.data); 
      }, 
      function(errResponse){ 
       console.error('Error while fetching Users'); 
       deferred.reject(errResponse); 
      } 
     ); 
     return deferred.promise; 
    } 
    return factory; 
}) 
+0

您在视图中我没有使用controllerAs语法。 – SaiUnique

+0

在你的控制器中,你正在获取数据。对? – Rakeschand

+0

尝试'' – nmanikiran

回答

1

你不是在你的视图中使用controllerAs,试试下面的代码,如果你要数据控制器比这将正常工作。

<body ng-controller="patentCtrl as p"> 
    <tr ng-repeat="x in p.patents"> 
    <td>{{x.id}} h</td> 
    <td>{{x.applicationnumber}}</td> 
    <td>{{x.clientref}}</td> 
    <td>$ {{x.costtorenew}}</td> 
    <td>{{x.renewalduedate}}</td> 
    <td><button type="button" class="btn btn-danger" ng-click="remove(x.id)">Remove</button></td> 
    </tr> 
</body> 
+0

Ahhhh正确的方向。所以如果控制器正在接收数据,我需要使用controllerAs?它正在加载内容(如按钮),但不是数据本身 –

+0

是的,内容会因为静态内容而加载。你可以通过在你的控制器中使用$ scope来代替'self(var self = this;)'来检查,那么它也可以工作 – Rakeschand

+0

不,它是我的属性名称在JSON文件中。现在全部排序。感谢您的时间!我会在一分钟内接受 –