Javascript Angular.js-$scope未从setTimeout()更新
我的想法是在加载数据时显示一个正在加载的.gif文件,然后在加载完所有数据后,等待Javascript Angular.js-$scope未从setTimeout()更新,javascript,angularjs,Javascript,Angularjs,我的想法是在加载数据时显示一个正在加载的.gif文件,然后在加载完所有数据后,等待x一段时间,目前为3000毫秒,但会降低,然后在给定的时间量后,将data.show的值设为true,这样加载的.gif文件就不再显示,并且该表将被删除 那么,如何从HTML、ng show中的JavaScript中引用show 任何帮助都将不胜感激 谢谢:) 编辑:这项工作与此有关,以回答- HTML 东西 JavaScript 函数成功(响应){ controller.errors=response.dat
x
一段时间,目前为3000毫秒,但会降低,然后在给定的时间量后,将data.show的值设为true,这样加载的.gif文件就不再显示,并且该表将被删除
那么,如何从HTML、ng show中的JavaScript中引用show
任何帮助都将不胜感激
谢谢:)
编辑:这项工作与此有关,以回答-
HTML
东西
JavaScript
函数成功(响应){
controller.errors=response.data;
setTimeout(函数(){
$('#errorTable')。数据表({
“长度菜单”:[[6],[6]],
“dom”:“frtip”
});
$(“div.toolbar”).html(“”);
$scope.show=true;
}, 3000);
}
尝试添加$scope.show={data:false}代码>在控制器中问题在于您没有在开头定义$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);
}
函数成功(响应){
controller.errors=response.data;
$timeout(函数(){
$('#errorTable')。数据表({
“长度菜单”:[[6],[6]],
“dom”:“frtip”
});
$(“div.toolbar”).html(“”);
$scope.show=true;
}, 3000);
}
是我尝试的plunker,因为没有div.toolbar
,这意味着,$('div.toolbar')
将是null/undefined
,这导致该语句的指针为null,而下一个语句不会被执行,因为执行之前已经停止
简单的解决方案是将$scope.show.data=true
作为由setTimeout
@$scope.show.data=true调用的函数中的第一条语句代码><代码>$scope.show
之前未声明,您正在访问$scope.show.data
,这将导致未定义的错误。在使用它之前尝试声明它。可能的重复我已经将它添加到控制器中,我已经添加了console.log($scope.show.data)$scope.show.data=true;log($scope.show.data);对于代码,第一个显示false,第二个显示true,但它在HTML中没有引用它。scope.show=true
必须位于setTimeout
函数中,因为3秒钟后它将显示内容。当我把它放在setTimeout
函数中时,它就不再工作了。好的,setTimeout部分可以忽略,但是你签出plunker了吗?它在那里工作。还请验证html文件(ng应用程序、ng控制器等)是的,我检查了plunker,当我在setTimeout之外更改范围时它工作,但当它在里面时它不工作。它需要在内部,因为表必须首先呈现,这将允许dataTable()工作。如果您尝试使用$timeout,则需要将其注入控制器。我已经编辑了代码。你也可以检查开发者控制台,让我知道你得到了什么错误。这可能与angularjs完全无关。编辑我的代码使用$timeout使用$timeout工作!
<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>
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);
}