最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - AngularJS - showhide elements from rootscope - Stack Overflow

programmeradmin0浏览0评论

I am listening for an application losing network connectivity by adding an event listener.

When the app goes offline i need to show a message.

The following code does not seem to work for me.

I add the event listener in the application run method so that it is globally available:

document.addEventListener("offline", function() {
    $rootScope.offline = true;
}, false);

Then in my index.html I show hide a message based on that $rootScope variable:

<div id="network-msg" ng-show="$root.offline">
    <div class="full-overlay" ng-show="$root.offline">
        <p class="txt-center">No internet connection</p>
        <p class="txt-center">Trying to re-connect</p>
    </div>
</div>

When i go offline i can see that the varibale is being updated but the message does not show. So if i output:

{{$root.offline}}

on page i can see it switch from false to true correctly but still message is not shown.

I am listening for an application losing network connectivity by adding an event listener.

When the app goes offline i need to show a message.

The following code does not seem to work for me.

I add the event listener in the application run method so that it is globally available:

document.addEventListener("offline", function() {
    $rootScope.offline = true;
}, false);

Then in my index.html I show hide a message based on that $rootScope variable:

<div id="network-msg" ng-show="$root.offline">
    <div class="full-overlay" ng-show="$root.offline">
        <p class="txt-center">No internet connection</p>
        <p class="txt-center">Trying to re-connect</p>
    </div>
</div>

When i go offline i can see that the varibale is being updated but the message does not show. So if i output:

{{$root.offline}}

on page i can see it switch from false to true correctly but still message is not shown.

Share Improve this question asked Oct 16, 2013 at 6:36 RyanP13RyanP13 7,75328 gold badges102 silver badges171 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

Since the value is updated in a dom event handler, the changes have to be done within a $apply callback

document.addEventListener("offline", function() {
    $rootScope.$apply(function(){
        $rootScope.offline = true;
    })
}, false);
发布评论

评论列表(0)

  1. 暂无评论