0
我正在向数组添加新数据,但它不会立即显示,当我添加新数据时,信息表会消失,刷新后会显示所有数据。为什么我的数据在我的表格中显示不正确,我如何推送数据以便自动显示?自动显示推送内容角度
我AddInventory功能
$scope.addInventory = function(inventory) {
$http.post("http://localhost/api/v1/inventory/", inventory)
.success(function(data) {
$scope.info = data;
});
};
输入表单添加新的数据
<input type="text" ng-model="inventory.name" />
<input type="text" ng-model="inventory.description" />
<select ng-model="inventory.category" ng-options="category.name for category in category.objects">
<option value="" selected>--Please select your category--</option>
</select>
<!--<input type="text" ng-model="inventory.category.name" />-->
<input type="button" ng-click="addInventory(inventory)" value="Save" />
我在这里展示我的数据
<table class="table table-hover">
<tr style="background-color: #f5f5f5;">
<th>Nosaukums</th>
<th>Apraksts</th>
<th>Kategorija</th>
</tr>
<tr ng-repeat="inventory in info.objects">
<td>{{inventory.name}}</td>
<td>{{inventory.description}}</td>
<td>{{inventory.category.name}}</td>
</tr>
它看起来像你用你的回调数据替换$ scope.info。如果ng-repeat没有找到info.objects,它将不会呈现任何东西。在任何情况下,您都应该将新数据推送到数组,而不是将其替换。 – Narretz
@Narretz有没有例子?当我使用push时,它在控制台推送对象中显示没有方法推送 – user3305175
如果您使用的是对象,那么您必须在对象集合中指定一个键。 info.objects.key =数据。但是,最好使用ng-repeat的数组。 – Narretz