我做了一個GET請求,找回了一個對象數組。用ng-repeat創建一個包含響應的表。表中的行數與對象的數量相匹配,但對象屬性(message.id)的值在表上不可見。ng-repeat在沒有innerHTML的情況下創建元素
<!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>
控制檯日誌:
請問您是否可以從'console'共享'$ scope.names'的內容? – manish
試用$ scope.names = []; –