5

我正在为 AngularJS 使用智能表 ( http://lorenzofox3.github.io/smart-table-website/ ),并且我创建了一个名为 isReset 的标志,它将触发表重新加载。发生这种情况是因为我有一个监视标志的指令,并且在设置 isReset 时会运行刷新,并且在刷新完成后,它将再次关闭标志。

我的问题是,当我设置标志时,它第一次运行,但是在监视标志的行为之后,它似乎永远不会设置回 false。我尝试手动将标志设置为 false,但下一次 $watch 甚至没有触发。我的代码如下,如果您能帮助我阐明这个问题,那就太好了。最奇怪的是,我在另一个地方以完全相同的方式使用它,并且它按预期工作。

JS

        $scope.resetFilter = function() {
        $scope.timestampFilter = "";
        $scope.levelFilter = "";
    };

    $scope.getAPIServerLogs = function (tableState) {
        $scope.isLoading = true;
        ServerLog.get({
            "serverType": "API",
            "timestampFilter": $scope.timestampFilter,
            "levelFilter": $scope.levelFilter,
            "offset": tableState.pagination.start,
            "limit": tableState.pagination.number,
            "sortField": tableState.sort.predicate,
            "order": tableState.sort.reverse ? "desc" : "asc"
        }, function (response) {
            $scope.isLoading = false;
            $scope.serverlogs = response.data;
            $scope.displayedserverlog = [].concat($scope.serverlogs);
            tableState.pagination.numberOfPages = response.pages;
        });
    };

指示

directives.directive('stReset', function () {
return {
    require: '^stTable',
    replace: false,
    scope: {stReset: "=stReset"},
    link: function (scope, element, attr, ctrl) {
        scope.$watch("stReset", function () {
            if (scope.stReset) {
                // reset scope value
                var tableState = ctrl.tableState();
                tableState.pagination.start = 0;
                tableState.sort.prediate = {};
                tableState.search = {};
                ctrl.pipe();
                scope.stReset = false;
            }
        }, true);
    }
};

HTML

<table st-table="displayedserverlog" st-safe-src="serverlogs" st-pipe="getAPIServerLogs"
   class="table table-striped table-hover logtable">
<thead st-reset="isReset">
<tr>
    <th st-sort-default="reverse" st-sort="timestamp" width="11%">Timestamp</th>
    <th st-sort="logger" width="30%">logger</th>
    <th st-sort="level" width="3%">Level</th>
    <th st-sort="thread" width="11%">Thread</th>
    <th st-sort="message" width="45%">Message</th>
</tr>
</thead>
<tbody ng-repeat="serverlog in serverlogs">
<tr ng-click="click(serverlog)" ng-class="{'tr-active':serverlog.isClicked, 'pointer danger':serverlog.exception}">
    <td>{{serverlog.timestamp | date: 'yyyy-MMM-dd hh:mm:ss'}}</td>
    <td>{{serverlog.logger}}</td>
    <td>{{serverlog.level}}</td>
    <td>{{serverlog.thread}}</td>
    <td>{{serverlog.message}}</td>
</tr>
<tr ng-show="serverlog.isClicked">
    <td colspan="6">
        <div class="row">
            <div class="col-md-12">
                <div>{{serverlog.exception}}</div>
                <pre><div ng-repeat="trace in serverlog.stacktrace track by $index" class="stacktrace">{{trace}}
                </div></pre>
            </div>
        </div>
    </td>
</tr>
</tbody>
<tfoot ng-hide="isLoading">
<tr>
    <td colspan="10" class="text-center">
        <div st-pagination="" st-items-by-page="50"></div>
    </td>
</tr>
</tfoot>

4

3 回答 3

0

这个 plunker 模拟你的问题:http ://plnkr.co/edit/c8crhe9ZR44GQBJ2sqm6?p=preview (看看控制台)

    scope.$watch("flag", function(neww, old){
            count ++;
            console.log("inWatch " + count + ": " + neww + ', ' + old);
            if (scope.flag === true) {
                scope.flag = false;
            }
    });

在 $watch 中将标志设置为 false 基本上意味着它将始终为 false(因为:您修改值 --> $watch 运行 --> 在函数的末尾将值设置为 false --> 值为 false)

于 2015-06-21T21:12:38.087 回答
0

我发现了一个解决方案。仍然不确定它为什么有效,但我添加了

scope.$parent.$parent.isReset = false;

在指令的末尾,它按照预期的方式工作。但是,替换现有的

scope.stReset = false;

打破了我正在使用该指令的另一个地方。现在,我将两者都做。将来当我在 AngularJS 上变得更聪明时,我会重新审视这个问题。我希望这对将来的某人有所帮助,这样他们就不会像我一样浪费 3 天时间来弄清楚它。

于 2015-06-22T14:47:49.743 回答
0

尝试这个。

var watcher = $scope.$watch('someScope', function(newValue, oldValue){
    if(newValue === 'yourValue') {
      watcher(); // stop this watch
    }
  });
于 2016-05-26T10:33:08.323 回答