【问题标题】:Unable to Populate Values from REST API using ng-repeat in Angular JS无法在 Angular JS 中使用 ng-repeat 从 REST API 填充值
【发布时间】:2016-11-29 20:24:58
【问题描述】:

我正在从 Angular JS 进行 Rest 调用。一旦我得到 API 的响应。我计划使用 ng-repeat 在前端填充消息。这是我的控制器端调用。

     $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");
                });

这是我在控制台上看到的回复

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

我想要做的是访问该消息并在前端填充。我正在使用 ng-repeat 来执行它并且它失败了。

这是我的 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> 

【问题讨论】:

    标签: html angularjs spring


    【解决方案1】:

    在您的 API 调用响应中,您的“dbresponse”似乎是一个数组:

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

    而在您的 html 中,您将其作为具有属性的单个对象进行访问:

    如果你的 'responseData' 是一个数组,你可能想在你的 html 中这样对待它。如果不是这样,请提供更多关于 html 的外观和“$scope”包含什么的信息。

    【讨论】:

    • 嘿 Filipe,这是一个错字。我正在尝试像这样使用 ng-repeat 。我正在填充这样的范围 $scope.dbresponse = responseData; .范围的内容,我已经把它和问题一起发布了
    猜你喜欢
    • 2020-11-30
    • 2014-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-15
    • 1970-01-01
    相关资源
    最近更新 更多