繁体   English   中英

Angular.js - $ scope不从setTimeout()更新

[英]Angular.js - $scope not updating from setTimeout()

我的想法是,我在加载数据时显示加载的.gif文件,然后在加载所有数据后,等待x个时间,当前为3000毫秒,但是会降低,然后在给定的时间后,设置data.show的值为true,因此加载的.gif不再显示而且表格是。

所以,我怎么引用show在HTML,NG-显示从JavaScript的?

任何帮助,将不胜感激!

谢谢 :)

编辑:这适用于此回答 - setTimeout v $ timeout

HTML

 <div ng-show="!show">
    <img id="loader" src="resources/ajax-loader.gif">
</div>

<div ng-show="show">
    <div class="ui grid center aligned">
        <div class="column thirteen wide">
            <table id="errorTable" class="ui compact celled definition table">
                <p>Stuff</p>
            </table>
        </div>
    </div>
</div>

JavaScript的

function onSuccess(response) {
    controller.errors = response.data;
    setTimeout(function () {
        $('#errorTable').DataTable({
            "lengthMenu": [[6], [6]],
            "dom": '<"toolbar">frtip'
        });
        $("div.toolbar").html('<div class="ui input left floated"><input type="text" placeholder="Search..."></div>');
        $scope.show = true;
    }, 3000);
}

问题是你没有在开头定义$ scope.show。 您可能不需要show.data。 请在控制器的开头添加以下行

$scope.show = false;

并将html更改为:

 <div ng-show="!show">
    <img id="loader" src="resources/ajax-loader.gif">
</div>

<div ng-show="show">
    <div class="ui grid center aligned">
        <div class="column thirteen wide">
            <table id="errorTable" class="ui compact celled definition table">
                <p>Stuff</p>
            </table>
        </div>
    </div>
</div>

在控制器中:

您已经在setTimeout外添加了$ scope.show.data,根据需要将其添加到setTimeout函数中并添加:

function onSuccess(response) {
    controller.errors = response.data;
    $timeout(function () {
        $('#errorTable').DataTable({
            "lengthMenu": [[6], [6]],
            "dom": '<"toolbar">frtip'
        });
        $("div.toolbar").html('<div class="ui input left floated"><input type="text" placeholder="Search..."></div>');
        $scope.show = true;
    }, 3000);
}

是我试过的那个傻瓜

尝试添加$scope.show = { data: false }; 在控制器中

因为没有div.toolbar ,意思是, $('div.toolbar')将为null/undefined ,这导致该语句处的空指针并且下一个语句未被执行,因为执行在之前已经停止。

简单的解决方案可能是放置$scope.show.data = true; 作为setTimeout调用的function的第一个语句。

暂无
暂无

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

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