Angularjs ng-show 不在模式内工作

标签 angularjs

单击一个按钮即可打开模式弹出窗口。模式弹出窗口正在打开,我想在模式弹出窗口中显示一个进度条。收到回复后我想隐藏它。

如果我关闭并再次打开弹出窗口,那么进度条第一次不会显示。

下面是我的 Controller 代码:

$scope.clickUpload = function(){
   $('#import_poi_modal').modal('show');

   setTimeout(function(){ 
      $scope.fileChangeProgress = true;
      console.log($scope.fileChangeProgress)
   },1000);
}

HTML:

<div class="modal fade" id="import_poi_modal">
   <div class="modal-dialog">
      <div class="modal-content">

         <div class="modal-body">

            <div class="center-content" ng-show="fileChangeProgress"> //here im using the variable to show and hide
                <div class="col-6">
                    <div class="progress m-t-30 m-b-30" >
                        <div class="progress-bar progress-bar-orange active progress-bar-striped" style="width: 100%; height:14px;" role="progressbar"> </div>
                    </div>
                </div>
            </div>


        </div>
      </div>
   </div>
</div>

我尝试过setTimeout。仍然不起作用。

最佳答案

由于 setTimeout 是一个 JavaScript 函数(不是 Angular 函数),因此您需要使用 $apply 来通知 Angular 发生更改。

setTimeout(function(){
  $scope.$apply(
    function() {
      $scope.fileChangeProgress = true;
      console.log($scope.fileChangeProgress);
    }
  );
},1000);

另一个解决方案是使用 Angular 原生 $timeout 支持:

$timeout(function(){ 
  $scope.fileChangeProgress = true;
  console.log($scope.fileChangeProgress)
},1000);

应该为您提供相同的目的,无需 $apply。

也可以引用这个$apply in setTimeout

关于Angularjs ng-show 不在模式内工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56042368/

相关文章:

AngularJS:两个具有单独 ngModel 的输入,一个更新另一个的值

html - 我的 anchor 链接不会从页面顶部开始

javascript - 动态添加的 Angular Material 输入没有得到样式

javascript - AngularJS:按正则表达式过滤

javascript - Angular 新路由器将 $scope 注入(inject) Controller

javascript - 如何在angular js中自动大写输入字段?

javascript - 如何修改 Promise 的属性以使其等于 AngularJS 中对象的属性?

javascript - 如何使用 TypeScript 和 Angular 2 向 ng2-ckeditor 添加插件?

angularjs satellizer 重定向回上一页

javascript - Protractor 和 Jasmine 永远不会解决获取网页标题的 promise