简体   繁体   English

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

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

The idea is that I show a loading .gif file while I load the data and then once all the data has loaded, wait an x amount of time, currently 3000ms, but will be lowered, then after the given amount of time, set the value of data.show to true, so that the loading .gif is no longer showing and that the Table is. 我的想法是,我在加载数据时显示加载的.gif文件,然后在加载所有数据后,等待x个时间,当前为3000毫秒,但是会降低,然后在给定的时间后,设置data.show的值为true,因此加载的.gif不再显示而且表格是。

So, how do I reference show from the JavaScript in the HTML, ng-show? 所以,我怎么引用show在HTML,NG-显示从JavaScript的?

Any help would be appreciated! 任何帮助,将不胜感激!

Thanks :) 谢谢 :)

edit: This works regarding to this to answer - setTimeout v $timeout 编辑:这适用于此回答 - setTimeout v $ timeout

HTML 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 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);
}

The problem is you have not defined $scope.show in the beginning. 问题是你没有在开头定义$ scope.show。 You probably don't need show.data . 您可能不需要show.data。 Please add the following line at the beginning of the controller 请在控制器的开头添加以下行

$scope.show = false;

And Change html to: 并将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>

And in controller: 在控制器中:

You have added the $scope.show.data outside setTimeout bring it within the setTimeout function as per your need and add: 您已经在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);
}

Here is the plunker that I tried 是我试过的那个傻瓜

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

As there is no div.toolbar , meaning, $('div.toolbar') would be null/undefined , which leads to a Null Pointer at that statement and the next statement is not being executed, as the execution has stopped just before. 因为没有div.toolbar ,意思是, $('div.toolbar')将为null/undefined ,这导致该语句处的空指针并且下一个语句未被执行,因为执行在之前已经停止。

Simple solution could be to place the $scope.show.data = true; 简单的解决方案可能是放置$scope.show.data = true; as the first statement in the function called by setTimeout . 作为setTimeout调用的function的第一个语句。

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

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