简体   繁体   English

带有异步角化的角度$ uibmodal解析

[英]Angular $uibmodal resolve with asynchornization

I have a service that gets data from a server. 我有一项从服务器获取数据的服务。 After retrieving data successfully, I would pass to a $uimodal in a directive. 成功检索数据后,我将在指令中传递给$ uimodal。 My code is described below 我的代码如下所述

Problem is when I run my app first time, console log shows consequence like 问题是当我第一次运行我的应用程序时,控制台日志显示如下结果

  1. debug sizeList outside // output >> undefined 调试sizeList外部// output >> undefined
  2. resolve // output >> undefined 解析// output >> undefined
  3. GeneralHelperService getSize // output >> [Object, Object] GeneralHelperService getSize // output >> [Object, Object]
  4. debug sizeList inside // output >> [Object, Object] 内部的debug sizeList // output >> [Object, Object]

I speculate that is due to a synchronization causing (3) and (4) to happen after (1) and (2). 我推测这是由于同步导致(3)和(4)在(1)和(2)之后发生。 My question is 我的问题是

  1. How to make (1) and (2) happen after (3) and (4) instead to guarantee (2) have data 如何使(1)和(2)在(3)和(4)之后发生以保证(2)有数据
  2. is there any proper way to implement rather than my approach 有什么合适的方法可以实现而不是我的方法

Thank you 谢谢

Service 服务

angular.module('myApp')
  .service('GeneralHelperService', function($http, $q) {
    var model = this;
    var uniqueSizes;
    model.getSizeList = function() {
        var url = 'api/size/' + input;
        if (uniqueSizes) return $q.when(uniqueSizes);
        return $http.get(url).then(function (data) {
            console.log('GeneralHelperService getSize', data.data);
            return uniqueSizes = data.data;
        })
    };
  }

Directive 指示

angular.module('myApp')
    .directive('aDirective', function ($modal) {
      return {
        restrict: 'E',
        controller: function ($scope) {},
        link: function (scope, el, attrs, modal) {
          GeneralHelperService.getSizeList().then(function (sizeList) {
            scope.sizeList = sizeList;
            console.log('debug sizeList inside', JSON.stringify(scope.sizeList));
          });
          console.log('debug sizeList outside', JSON.stringify(scope.sizeList));
          var modalInstance = $modal.open({
            template: '<div>{{sizeList}}</div>',
            size: 'sm',
            controller: 'ControlModal as controlModal',
            resolve: {
              sizeList: function () {
                console.log('resolve', scope.sizeList);
                return scope.sizeList;
              }
            }
          });
        }

You slightly over complicated your codes, as modal resolve basically helps to resolve async (promises) before creating modal window, so change your resolve of $modal.open to 您稍微复杂了一些代码,因为modal解析基本上有助于在创建模态窗口之前解析异步(承诺),因此请将$modal.openresolve更改为

resolve: {
   sizeList: function(GeneralHelperService) {
       return GeneralHelperService.getSizeList();
   }
}

Then in your controller ControlModal you should be able to access the return data via sizeList and don't forget dependency injection sizeList in your controller. 然后,在控制器ControlModal您应该能够通过sizeList访问返回数据,并且不要忘记控制器中的依赖项注入sizeList

Btw, just noticed in your $http.get you return uniqueSizes = data.data; 顺便说一句,只是在您的$http.get注意到,您return uniqueSizes = data.data; , make sure you return data.data; ,请确保您return data.data;

Here is a simple JSFiddle to get the idea 这是一个简单的JSFiddle来理解

put

var modalInstance = $modal.open({
        template: '<div>{{sizeList}}</div>',
        size: 'sm',
        controller: 'ControlModal as controlModal',
        resolve: {
          sizeList: function () {
            console.log('resolve', scope.sizeList);
            return scope.sizeList;
          }
        }
      })

inside .then of asynchronus call. 在.then内部进行异步调用。 Because its not poosible to put that outside ie parallel to asynchronus call 因为将其放在外部即与异步调用并行是不可能的

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

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