【问题标题】:Angular-ui modal, sending data into modal controller from $httpAngular-ui modal,从 $http 发送数据到 modal 控制器
【发布时间】:2013-09-23 04:30:39
【问题描述】:

我正在使用 angular-ui 模态指令http://angular-ui.github.io/bootstrap/

我已按照上面链接中的示例进行操作。

这是我想从我的控制器发送的数据:

ProductsFactory.getOneProduct().then(function(d){
  $scope.selectedProduct = d.data;
});

$scope.open = function () {
  var modalInstance = $modal.open({
    controller: 'ModalInstanceCtrl',
    templateUrl: 'productDetail.html',
    resolve: {
      items: function () {
        return $scope.selectedProduct;
      }
    }
  });
};

这是我的模态控制器:

var ModalInstanceCtrl = function ($scope, $modalInstance, selectedProduct) {

  console.log(selectedProduct);

  $scope.ok = function () {
    $modalInstance.close();
  };

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

问题是我无法访问模态控制器中的“选定产品”。我知道原因是做宽度异步调用,它只能从 GUI 访问。但是我该如何解决这个问题呢?如何将“$scope.selectedProduct”发送到我的 ModalInstanceCtrl?

【问题讨论】:

    标签: angularjs angular-ui


    【解决方案1】:

    你可以试试

    $scope.open = function () {
      var modalInstance = $modal.open({
        controller: 'ModalInstanceCtrl',
        templateUrl: 'productDetail.html',
        resolve: {
          items: function () {
            return ProductsFactory.getOneProduct();
          }
        }
      });
    };
    

    基本上你的$modal 可以接受承诺,所以为什么不使用它。现在,当 promise 得到解决时,该对象应该在控制器上可用。 ModalInstanceCtrl 应该是

    var ModalInstanceCtrl = function ($scope, $modalInstance, items) {
    

    因为您正在解析 items 属性而不是 selectedProduct 属性。

    【讨论】:

    • 你能帮忙解释一下这个变化吗?原始帖子的问题是该成员将是“items”而不是“selectedProduct”。原始示例会将产品数据放入该项目成员中。这个答案改变了将承诺放在该项目成员中。为什么要改变这个?你说“可以做到”,但由于这不是问题,我不明白这种变化的原因。我假设必须更改代码(不在图片中),因为现在您有一个承诺而不是数据——或者是否有其他一些隐藏的魔法使两个示例相同?
    • 您可以在不进行此更改的情况下工作。唯一需要确定的是 $scope.selectedProduct 是在调用模态对话框之前设置的。考虑到getOneProduct 调用是异步的。在resolve 中使用getOneProduct 可以避免时间问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-25
    • 2017-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多