【问题标题】:AngularFire Pause / Resume SyncAngularFire 暂停/恢复同步
【发布时间】:2014-02-24 16:32:55
【问题描述】:

是否有人能够使用 Angular fire 暂停/恢复同步?

Firebase/AngularJS (Angularfire) 的新手。

我尝试过清空对象并通过单击按钮重新初始化它们,但它没有做任何事情,

感谢您的任何意见。

史蒂夫

按要求编码

var app = angular.module("imageanalyser-app", ["firebase"]);


var ref = null;
function MyController($scope, $firebase) {
    ref = new Firebase("...");
    $scope.messages = $firebase(ref.endAt().limit(100));

}
function resumeSync() {
    $("#btnResume").click(function() {
        ref = new Firebase("...");
    });
}

function stopSync() {
    $("#btnStop").click(function () {
        ref = null;
    });
}

<div class="row">
<div class="col-md-10 col-md-offset-1">
    <div ng-app="imageanalyser-app">
        <div id="links" class="container-fluid" ng-controller="MyController">

            <div ng-repeat="msg in messages" class="gallery-item-container">
                <a href="{{msg.highResUrl}}"  data-gallery>
                    <img ng-src="{{msg.Source}}" height="20" width="20"  style="position: absolute"/>
                    <img ng-src="{{msg.imgUrl}}" class="gallery-item-image" />
                </a>
                <div class="gallery-item-tags" ng-repeat="tag in msg.tags">{{tag}}</div>

            </div>
        </div>
    </div>
</div>

【问题讨论】:

  • 你能把你的代码粘贴进去吗?
  • 完成了,抱歉应该放在第一位
  • 如果您持有原生 Firebase 参考,您可以使用 goOffline 和 goOnline 方法。参见文档@firebase.com/docs/javascript/firebase/gooffline.html
  • 这对我不起作用说它不包含方法 gooffline 或 online...奇怪

标签: javascript angularjs firebase angularfire


【解决方案1】:

您正在更新ref,但从未更改$scope.messages。 ng-repeat 基于$scope.messages,所以它也不会改变!

要让它按照你的意愿运行,你需要影响 messages 变量。

【讨论】:

  • 这可能是要走的路,但是……如果我将它设置为 null,我能做些什么来停止同步,所有绑定到它的东西都会从页面中删除。我找到了 $scope.messages.$off() 来停止同步.. 在firebase.com/docs/angular/reference.html 上找到了但是一种重新启动同步的方法?好像什么都没有
【解决方案2】:

这对我有用,使用 $off() 停止同步然后重新初始化它。

var fireBaseRef;
var synced = true;
function MyController($scope, $firebase) {
    fireBaseRef = new Firebase("......");
    $scope.messages = $firebase(fireBaseRef.endAt().limit(100));

    jQuery("#btnToggleState").click(function () {
        if (synced) {
            $scope.messages.$off();
            synced = false;
        } else {
            $scope.messages = $firebase(fireBaseRef.endAt().limit(100));
            synced = true;
        }

    });

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-28
    • 1970-01-01
    • 2020-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-05
    • 1970-01-01
    相关资源
    最近更新 更多