ng-repeat创建没有innerHTML

时间:2016-04-04 07:28:30

标签: angularjs angularjs-scope angularjs-ng-repeat

我做了一个GET请求,找回了一个对象数组。使用ng-repeat创建包含响应的表。表中的行数与对象数相匹配,但对象属性(message.id)的值在表中不可见。



<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.3/angular.min.js"></script>
    
</head>
<body>
<div class="container" ng-app="testApp">
    <div>
        <table >
        <tbody>
        <tr>
        <td style="vertical-align: top">
            <div ng-controller="msgCntrl">
            <table class="table">
                    <thead>
                    <tr>
                      <th>Id</th>
                      <th>User</th>
                      <th>Message</th>
                      <th>Delete</th>
                    </tr>
                    </thead>
                    <tbody>
                      <tr ng-repeat="message in names">
                        <td> {{message.user}}</td>
                        <td> {{message.content}} </td>      
                      </tr>
                    </tbody>
                </table>
              </div>
        </td>
          
        <td class="col-md-3" style="width:25%;padding-left:40px;vertical-align: top">
            <div ng-controller="otherController">
               <!-- this POST data to the application-->
            </div>
        </td>
        </tr>
        </tbody>
        </table>    
    </div>
</div>
<script>
        var app = angular.module("testApp", []);            
        app.controller("msgCntrl", function ($scope, $http) {
            $scope.names = null;
            $http.get('/api/message/').
                success(function(data) {
                    $scope.names = data.objects;
                    console.log($scope.names);// Printed the response properly
            });       
        });
</script>
</body>
</html>
&#13;
&#13;
&#13;

This is how the table looks like. The text is not visible

控制台日志:

enter image description here

2 个答案:

答案 0 :(得分:0)

您的语法似乎没问题,但请确保

     <td> {{message.user}}</td>
     <td> {{message.content}} </td>      

邮件的属性名称为user,内容和用户及内容都有一些价值,请分享console.log

答案 1 :(得分:0)

你能告诉我们你的成就:

console.log($scope.names);

也许你需要在你的HTMl标签中声明你的控制器,如:

<div class="container" ng-app="testApp">
    <div ng-controller="msgCntrl">
         ...
    </div>
</div>

希望它可以提供帮助。