我開始遇到這個問題很多。這個場景是我有一個我想重用的部分。問題是,我將不得不覆蓋範圍內的以前的數據,以便新數據顯示在部分內容中。我想知道人們如何解決這個問題。如何有效地重用部分?
下面是一個例子:
我有標籤,所有使用$scope.pebbles
並共享相同的部分。對於每個標籤,我希望填充不同的數據。所以很自然地,我只是提出新的數據請求並將其存儲回變量中以更改數據,例如:$scope.pebbles = getPebbles(color)
。這個問題是我每次更改標籤時都必須提出新的請求。有沒有一個更好的方法,而不是有一個很大的局部?
overview.html:
<tab>
<tab-heading>
<i class="icon-bell"></i> All
</tab-heading>
<div class="tab-content">
<div ng-include="'views/partials/_pebbles.html'"></div>
</div>
</tab>
<tab ng-repeat="color in colors">
<tab-heading ng-click="tab(color)">
{{color}}
</tab-heading>
<div class="tab-content">
<div ng-include="'views/partials/_pebbles.html'"></div>
</div>
</tab>
_pebbles.html:
<table class="table table-striped table-bordered bootstrap-datatable datatable">
<thead>
<tr>
<th>Pebble Name</th>
<th>Pebble Type</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="pebble in pebbles">
<td>{{pebble.name}}</td>
<td>{{pebble.type}}</td>
</tr>
</tbody>
</table>
控制器:
$scope.colors = ['blue','red','orange','purple']
$scope.pebbles = getPebbles() // http factory, makes a request for data
$scope.tab = function (color) {
$scope.pebbles = getPebbles(color)
}
jsfiddle在這種情況下非常有用。當你可以從一開始就開始玩代碼 –