【问题标题】:Angular JS ng-repeat not displaying valuesAngular JS ng-repeat不显示值
【发布时间】:2017-04-15 03:38:15
【问题描述】:

我正在从 angular 到 Spring 进行 REST 调用。我能够得到响应。当我尝试使用 ng-repeat 填充视图中的值时,屏幕上没有显示任何内容。

这是我的 Angular 代码 此代码在 showhidecontroller 中

  var resuls = $http.post('http://localhost:8080/aeservices/AddConfig', dataobj);
          resuls.success(function(data, status, headers, config) {
                $scope.dbresponse= data;
                $scope.messagetest = $scope.dbresponse[0].messages;
                alert($scope.messagetest);
                console.log( $scope.messagetest);
                console.log(data);

                });

这是我从 API 收到的回复

    [{
"data12": null,
"name": null,
"errors": ["error data1", "error data2"],
"messages": ["Configuration has been Added successfully", "Configuration has been Added successfully"]
}]

这是我的 HTML

     <table ng-controller="ShowHideController">
<tr ng-repeat="item in dbresponse[0].errors" >
        <span><td align="left" class="validationMsg"><img src="images/red_bullet.gif" border="0" width="8" height="8" alt="">&nbsp
{{item}}
</td></span></tr>
</table> 

我尝试使用 ng-repeat,将自己声明为 $scope.items = ['one','two','three','four'] 这样的项目。即使这也没有显示在 HTML 上。

【问题讨论】:

    标签: angularjs html spring-mvc model-view-controller


    【解决方案1】:

    尝试使用 data.data,

    控制器:

    app.controller("listController", ["$scope", "$http",
      function($scope, $http) {
        $http.get('test.json').then(function(response) {
          $scope.dbresponse = response.data;
        });
      }
    ]);
    

    HTML:

      <table ng-controller="listController">
          <tr ng-repeat="item in dbresponse[0].errors">
            <td align="left" class="validationMsg"><img src="images/red_bullet.gif" border="0" width="8" height="8" alt=""> {{item}}
            </td>
          </tr>
      </table>
    

    DEMO

    【讨论】:

    • 这与 op 发布的内容有何不同? (您的编辑现在更有意义)
    • @ForeignObject 是的,我在中间,希望现在更好
    • 嘿,你做的和我做的一样,请问我哪里错了
    • $scope.dbresponse = data.data;
    • 我现在按照你的建议做了同样的事情,但它仍然没有出现在视图中
    【解决方案2】:

    请尝试下面的代码,不要声明变量结果。

      $http.post('http://localhost:8080/aeservices/AddConfig', dataobj).success(function(data, status, headers, config) {
                    $scope.dbresponse= data;
                    $scope.messagetest = $scope.dbresponse[0].messages;
                    alert($scope.messagetest);
                    console.log( $scope.messagetest);
                    console.log(data);
    
                    });
    

    【讨论】:

    • 我试过这个并且能够获得警报,但我的问题是我无法在 html 上显示它
    • 当您在变量中进行 AJAX 调用时不会返回任何内容。
    • 我已经更改了我的代码并尝试了您提到的方式,但仍然无法正常工作。我希望我的控制器/html部分可能有问题
    • 在尝试 ng-repeat 之前。试试 $scope.test="preveen";并以 html 显示并让我知道它是否显示 praveen ?
    • 不,它没有显示出来
    【解决方案3】:

    发生的问题是我在 HTML 中声明了我的控制器两次

    【讨论】:

      猜你喜欢
      • 2016-11-28
      • 1970-01-01
      • 2017-09-09
      • 1970-01-01
      • 1970-01-01
      • 2014-11-10
      • 1970-01-01
      • 1970-01-01
      • 2017-06-09
      相关资源
      最近更新 更多