2016-11-29 55 views
0

我正在从Angular JS进行Rest调用。一旦我得到API的响应。我打算使用ng-repeat在前端填充消息。这是我的控制器端调用。无法使用Angular JS中的ng-repeat从REST API填充值

 $http({ 
      url: "http://localhost:8080/services/AddConfig", 
      method: "POST", 
      data: dataobj, 
      headers: { 
       'Content-Type': 'application/json' 
      } 
     }).success(function(data, status, headers, config){ 
       responseData = data; 
       $scope.dbresponse = responseData; 
       console.log(responseData); 
       console.log($scope.dbresponse[0].messages); 
       console.log($scope.dbresponse[0].errors); 
       if($scope.dbresponse[0].messages[0] != null){ 
        console.log("success message"); 

        console.log("After Setting it"); 
       }else if ($scope.dbresponse[0].errors != null){ 
        console.log("has errors throw validation message"); 
       } 
     }).error(function(error){ 
       alert("Please Try again"); 
       }); 

这是我的repsonse在控制台

Object  
    errors:null 
    messages:Array[1] 
      0:Added Successfully 

所看到我想要做的是访问该消息并使用NG重复做,它的失败在前端.I'm填充。

这是我的HTML

 <table> 
<tr ng-repeat="messages in dbresponse.messages"> 
<td align="left" class="validMsg"><img src="images/red_bullet.gif" border="0" width="8" height="8" alt="">&nbsp;{{messages}}</td></tr> 
</table> 

回答

1

在您的API呼叫响应似乎​​你的 'dbresponse' 是一个数组:

的console.log($ scope.dbresponse [0] .messages) ;

而在你的HTML你访问它带有属性的单个对象:

< TR NG重复= “在dbresponse.messages消息” >

如果你的“responseData '是一个数组,你可能想在你的html中把它当作对待。如果情况并非如此,请提供更多有关html的外观和'$ scope'包含的信息。

+1

嘿菲利普,这是一个错字。我正在尝试使用ng-repeat像这样 。我正在像这样填充范围$ scope.dbresponse = responseData; 。范围的内容,我已将它与问题一起发布 – Praveen