繁体   English   中英

$ watch只触发一次

[英]$watch only triggering once

我正在为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>

这个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 runs - >在函数结束时将值设置为false - > value为false)

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

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

在指令的最后,它按照预期的方式工作。 但是,取代现有的

scope.stReset = false;

打破了我使用该指令的另一个地方。 现在,我会做两件事。 将来,当我在AngularJS更聪明时,我将重新审视这个问题。 我希望这对未来的某个人有所帮助,所以他们不要浪费3天时间像我一样想出来。

尝试这个。

var watcher = $scope.$watch('someScope', function(newValue, oldValue){
    if(newValue === 'yourValue') {
      watcher(); // stop this watch
    }
  });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM