【问题标题】:resolve doesnt work in angular ui modal解决在角度 ui 模态中不起作用
【发布时间】:2016-01-18 01:48:35
【问题描述】:

我正在尝试使用 angular bootstrap ui 显示一个模态,它可以解析控制器中的项目。问题是我无法在模态控制器中获取那些已解决的项目。

$scope.showItemsInBill = function(bill){
  var modalInstance = $uibModal.open({
    animation: $scope.anismationsEnabled,
    resolve: {
      items: function(){
        var items = [];
        BillingService.getItemsInBill(bill).then(function(response){
          console.log(response);
          return response;
        });
      }
    },
    templateUrl: 'templates/showitems-modal.html',
    controller: 'ShowItemsModalCtrl',
    backdrop: 'static'
  });
}

模态控制器如下:

angular.controller('ShowItemsModalCtrl', ['$scope', '$uibModalInstance', 'BillingService', 'items', function ($scope, $uibModalInstance, BillingService, items) {

init();

function init(){
  console.log('Modal is initializing');
  console.log(items);
  $scope.items = items;
}

$scope.cancel = function () {
  $uibModalInstance.dismiss('cancel');
};

$scope.printBill = function(){
  console.log('printing bill');
}

}]);

我在控制台中得到的响应是:

history-controller.js:24 [Object]0: Objectlength: 1__proto__: Array[0]

showitems-modal-controller.js:8 模态正在初始化

showitems-modal-controller.js:9 未定义

所以我从这里了解到的是调用了服务的成功回调,但它没有解析到模态控制器中,而是初始化了模态控制器。为什么会这样?

是不是因为我从成功回调中返回响应。如果是这样,我该怎么办?

【问题讨论】:

    标签: javascript angularjs twitter-bootstrap angular-ui-bootstrap bootstrap-modal


    【解决方案1】:

    你需要在resolve中返回promise

    resolve: {
      items: function(){
        var items = [];
        // missing "return"
        return   BillingService.getItemsInBill(bill).then(function(response){
          console.log(response);
          return response;
        });
      }
    },
    

    【讨论】:

    • 我的成功回调中的返回是什么?这有什么用?
    • 返回值,以便下一个then 可以访问它。你看不到下一个then,但它在内部用于在控制器中创建注入的参数
    猜你喜欢
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多