请求前清除以前的数据

Wal*_*mar 5 javascript ajax asp.net-ajax duplicates angularjs

我有一个HTML模板,它从控制器的数组中加载数据并将它们保存在表中.我还有一个指令,它可以对表行进行转换.数据数组正在填写API请求.在新请求之后,我已将请求结果合并到我的表中.正在为每个请求添加一组行,而不是清除以前的结果.

我已经调试了我的控制器代码来检查我的数组的状态,并在每次请求之前清除它.这是肯定的.然而,之前添加了新的结果.我认为原因可能出在我的移植指令模板中.

模板看起来像:

<div class="row">
    <div class="col-md-12">
        <div id="results" ng-show="results.length > 0">
            <table class="table result-table">
                <thead>
                    <tr>
                        <th>1</th>
                        <th>2</th>
                        <th>3</th>
                        <th>4</th>
                    </tr>
                </thead>
                <tbody>
                    <tr ng-repeat="result in results" my-result></tr>
                </tbody>
            </table>
        </div>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

这是我的transclusion指令的代码:

angular.module('app').directive('myResult',
['$compile',
function ($compile) {
    return {
        transclude: true,
        templateUrl: '/Scripts/app/templates/resultTemplate.html',
        compile: function (tElement, tAttr, transclude) {
            var contents = tElement.contents().remove();
            var compiledContents;
            return function (scope, iElement, iAttr) {
                if (!compiledContents) {
                    compiledContents = $compile(contents, transclude);
                }
                compiledContents(scope, function (clone, scope) {
                    iElement.replaceWith(clone);
                });
            };
        }
    }
}]);
Run Code Online (Sandbox Code Playgroud)

最后用于翻译的模板:

<tr class="big-bord">
    <td colspan="4"><h3>{{result.fullName}}</h3></td>
</tr>
<tr ng-repeat="item in result.items">
    <td>{{item.value1}}</td>
    <td>{{item.value2}}</td>
    <td>{{item.value3}}</td>
    <td>{{item.value4}}</td>
</tr>
Run Code Online (Sandbox Code Playgroud)

如何在每个API请求之前清除我的结果?

Wal*_*mar 4

我解决了我的问题。事实证明,一张表中允许使用多个 <tbody> 标签。所以我只是将 ng-repeat 移至 <tbody> 标签:

<tbody ng-repeat="result in results">
    <tr class="big-bord">
        <td colspan="4"><h3>{{result.fullName}}</h3></td>
    </tr>
    <tr ng-repeat="item in result.items">
        <td>{{item.value1}}</td>
        <td>{{item.value2}}</td>
        <td>{{item.value3}}</td>
        <td>{{item.value4}}</td>
    </tr>
</tbody>
Run Code Online (Sandbox Code Playgroud)

所以,我根本不需要任何指令。