javascript - AngularJS中临时切换 View

标签 javascript angularjs

我的问题是我有一个 Controller ,我在其中调用一个函数,该函数基本上执行返回 promise 的 http 调用。

示例 Controller :

myApp.controller('MyController', function($scope) {
    MyService.doHttp().then(function(result) {
        handleMyData(result);
    });
});

在http调用期间,我想显示一个“正在加载” View /部分,其中包含一个加载微调器,然后在请求最终完成时转发到另一个 View 。 我怎么能做这样的事呢?感谢您的帮助。

最佳答案

我处理这个问题的方法是,在我的根 index.html 页面上,我有这样的东西:

<div class="modal-loading" ng-show="loading"></div>

然后,当加载某些内容时,我在 $rootScope 上设置一个 loading 属性,并在加载完成后隐藏它。

(function(app) {
    app.controller(‘MyController’, [‘$scope’, ‘$rootScope’, '$location', ‘MyService’, 
          function($scope, $rootScope, $location, MyService) {

    $scope.loadData = function() {
        $rootScope.loading = true;

        MyService.doHttp().then(function(result) {
           handleMyData(result);
           $rootScope.loading = false;
           $location.path('/anotherView');
        }); 
      }
    });

})(angular.module(‘app’));

作为引用,模态加载的css是这样的,它用较浅的颜色覆盖整个屏幕,中间有一个旋转gif

.modal-loading {
  display: block;
  position:   fixed;
  z-index:    1000;
  top:        0;
  left:       0;
  height:     100%;
  width:      100%;
  background: rgba( 255, 255, 255, .8 ) 
            url('../images/ajax-loader.gif') 
            50% 50% 
            no-repeat;
}

关于javascript - AngularJS中临时切换 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22787556/

相关文章:

javascript - 如何防止ESC按键断线?

javascript - 处理同一页面中多种表单的 Angular ng-model

node.js - AngularJS 应用程序在本地和 Heroku 上有不同的行为

javascript - AngularJS <a> 标签链接不起作用

AngularJS - ng-click 的滑动效果

javascript - 删除数组中所有重复项和未定义项。 Ramda.js

javascript - html 文件类型图像上传后增加文本计数器

javascript - 在 JS 确认()方法中使用 PHP 变量

javascript - 如何在 python django 中返回字典并在 javascript 中查看它?

javascript - 带有 API 和 MONGOOSE 的简单 Web SPA(ID 问题)