简体   繁体   English

angularjs ng-show延迟显示div

[英]angularjs ng-show delay in showing div

I am fairly new to Angular. 我是Angular的新手。 I have a file input 我有文件输入

<input type="file" id="sampleFile" onchange="angular.element(this).scope().vm.displaySelectedXml()" />

with a div below it: 下方有一个div:

<div ng-hide="!vm.displayXml">
    <div> xml file display goes here </div>
</div>

I got the onchange event handler from here . 我从这里得到了onchange事件处理程序。

My controller js has the following function: 我的控制器js具有以下功能:

function displaySelectedXml() {
    vm.displayXml = true;
}

So when a file is selected (that is different from the previous, or the first one) the onchange should trigger and call the fn above, which sets the flag to true, which should then show the contents of the div. 因此,当选择一个文件(与前一个文件或第一个文件不同)时,onchange将触发并调用上面的fn,这会将标志设置为true,然后应显示div的内容。

The problem is that it takes about 30 to 60 secs for the div to display. 问题在于,div显示大约需要30到60秒。 When I set the breakpoint in the function above and select a file, the function is called right away. 当我在上面的函数中设置断点并选择文件时,该函数立即被调用。 The file I select is less than 1k, so it's very small. 我选择的文件小于1k,因此非常小。 It seems that the delay is occurring somewhere in the Angular layer. 似乎延迟发生在Angular层中的某个位置。 Could this be the case? 可能是这样吗? Anybody know why this could be happening? 有人知道为什么会这样吗? Thanks in advance. 提前致谢。 This happens in both IE and Chrome. IE和Chrome都会发生这种情况。

Problem here is that you change a scope variable from outside, which angular is not able to recognize. 这里的问题是您从外部更改范围变量,该角度变量无法识别。 The change will only be applied in the next diggest cycle. 更改将仅在下一个摘要周期中应用。 You can fix this by adding a $scope.$apply : 您可以通过添加$scope.$apply来解决此问题:

onchange="var scope = angular.element(this).scope(); scope.vm.displaySelectedXml(); scope.$apply()"

实现相同效果的更简洁的方法:

<input type="file" id="sampleFile" ng-change="vm.displaySelectedXml()">

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

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