简体   繁体   English

用来自REST请求的数据显示angular-ui模态(有延迟)

[英]Display angular-ui Modal with data from a REST request (with delay)

I want to open an Modal Dialog (angular-ui) , but when the open() function is called, the data are not available. 我想打开一个模态对话框(angular-ui) ,但是当调用open()函数时,数据不可用。 Data are loaded by a resource call so there's a delay. 数据是通过资源调用加载的,因此会有延迟。

I tried to play with the promise opened , but data are not changed. 我尝试将诺言opened ,但数据没有更改。

  var modalInstance = $modal.open({
      templateUrl: 'myModalContent.html',
      controller: ModalInstanceCtrl,
      resolve: {
        mydata: function() {
          return "Loading...";
        }
      }
    });

    modalInstance.opened.then(function() {
          $scope.mydata = $scope.loadData();
        }, function() {
          $log.info('Modal dismissed at: ' + new Date());
        });
      };


     $scope.loadData = function() {
        $timeout( function(){
          $log.info("data loaded");
          return "data loaded...";
        }, 3000)
      };

Something is missing in my understanding between the resolve property, the modal promises and the deferred loading. 在我的理解中,在resolve属性,模式承诺和延迟加载之间缺少一些东西。

(I would like to use restangular to load the resource). (我想使用restangular来加载资源)。

Here is the sample : http://plnkr.co/edit/Mj6JolD06DUJd6N6ECYi 这是示例: http : //plnkr.co/edit/Mj6JolD06DUJd6N6ECYi

Thanks in advance for any clue 预先感谢您提供任何线索

You are mostly there. 您大部分在那儿。 The problem is in the way you coded the loadData function. 问题在于您编码loadData函数的方式。 Since you are doing an asynchronous call you can't just do a return of data like that. 由于您正在执行异步调用,因此您不能仅返回类似的数据。 Instead, what you can do is in your loadData you can call a function on the modalInstance that will set a value in the $scope of the modal. 相反,您可以做的是在loadData您可以在modalInstance上调用一个函数,该函数将在模态的$ scope中设置一个值。

So in your ModalInstanceCtrl you can add a function like this: 因此,在您的ModalInstanceCtrl中,您可以添加如下函数:

$modalInstance.setMyData = function(theData) {
  $scope.mydata = theData;
};

And then you can call that in your loadData like this: 然后您可以像这样在loadData中调用它:

$scope.loadData = function(aModalInstance) {
  $log.info("starts loading");
  $timeout(function() {
    $log.info("data loaded");
    aModalInstance.setMyData("data loaded...");
  }, 3000);
};

You also need to make sure that you pass the instance of the modal when you call loadData: 您还需要确保在调用loadData时传递模态实例:

modalInstance.opened.then(function() {
  $scope.loadData(modalInstance);
}, function() {
  $log.info('Modal dismissed at: ' + new Date());
});

I created an updated plunk so you can see how it works: http://plnkr.co/edit/M7qfegYIOqOQekoxLaj5?p=preview 我创建了一个更新的插件,以便您了解它的工作原理: http : //plnkr.co/edit/M7qfegYIOqOQekoxLaj5?p=preview

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

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