javascript - $scope.$watch only triggered once - Stack Overflow

I've set the following watcher in my controller:var embeds = {twitter: false, facebook: false};$sc

I've set the following watcher in my controller:

var embeds = {twitter: false, facebook: false};

$scope.$watch(embeds, function(newVal, oldVal) {
    if(embeds.twitter && embeds.facebook) $scope.loading = appLoader.off();
});

This should fire when embeds changes. I have the following functions that check if all my embedded Tweets and Facebook posts have loaded for the page. When all Tweets or Facebook posts are loaded, it updates embeds within a $timeout block in order to trigger a digest cycle.

checkFBInit();

twttr.ready(function(twttr) {
    twttr.events.bind('loaded', function(event) {
        $timeout(function() {
            embeds.twitter = true;
        });
    });
});

function checkFBInit() {
    // Ensure FB.init has been called before attempting to subscribe to event
    var fbTrys = 0;
    function init() {
        fbTrys++;
        if (fbTrys >= 60) {
            return;
        } else if (typeof(FB) !== 'undefined') {
            fbTrys = 60;
            FB.Event.subscribe('xfbml.render', function() {
                $timeout(function() {
                    embeds.facebook = true;
                });
            });
            return;
        } else {
            init();
        };
    };
    init();
};

The problem I'm having is that my watcher only fires once when I set it. I've tried binding embeds to $scope and/or watching embeds.twitter and embeds.facebook but the watcher only ever fires once.

I've set the following watcher in my controller:

var embeds = {twitter: false, facebook: false};

$scope.$watch(embeds, function(newVal, oldVal) {
    if(embeds.twitter && embeds.facebook) $scope.loading = appLoader.off();
});

This should fire when embeds changes. I have the following functions that check if all my embedded Tweets and Facebook posts have loaded for the page. When all Tweets or Facebook posts are loaded, it updates embeds within a $timeout block in order to trigger a digest cycle.

checkFBInit();

twttr.ready(function(twttr) {
    twttr.events.bind('loaded', function(event) {
        $timeout(function() {
            embeds.twitter = true;
        });
    });
});

function checkFBInit() {
    // Ensure FB.init has been called before attempting to subscribe to event
    var fbTrys = 0;
    function init() {
        fbTrys++;
        if (fbTrys >= 60) {
            return;
        } else if (typeof(FB) !== 'undefined') {
            fbTrys = 60;
            FB.Event.subscribe('xfbml.render', function() {
                $timeout(function() {
                    embeds.facebook = true;
                });
            });
            return;
        } else {
            init();
        };
    };
    init();
};

The problem I'm having is that my watcher only fires once when I set it. I've tried binding embeds to $scope and/or watching embeds.twitter and embeds.facebook but the watcher only ever fires once.

Share Improve this question asked Mar 5, 2016 at 18:43 Daniel BonnellDaniel Bonnell 5,0079 gold badges51 silver badges92 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

Use:

$scope.embeds = {twitter: false, facebook: false};
$scope.$watch('embeds', function(newVal, oldVal) {
    if ($scope.embeds.twitter && $scope.embeds.facebook) {
        $scope.loading = appLoader.off();
    }
}, true);

See https://docs.angularjs/api/ng/type/$rootScope.Scope. First argument must be string or function which return the name of param.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744632301a4584935.html

相关推荐

  • javascript - $scope.$watch only triggered once - Stack Overflow

    I've set the following watcher in my controller:var embeds = {twitter: false, facebook: false};$sc

    2天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信
['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>