【发布时间】:2013-07-30 04:05:42
【问题描述】:
我开始经常遇到这个问题。场景是我有一个我想重用的部分。问题是,我必须覆盖范围内以前的数据,以便新数据显示在部分中。我想知道人们是如何解决这个问题的。
这是一个例子:
我的标签都使用$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 在这种情况下非常有用。什么时候可以从头开始玩代码
标签: angularjs