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