Angular-ui模态,从$ http发送数据到模态控制器

时间:2022-06-09 11:22:11

I'm using the angular-ui modal directive http://angular-ui.github.io/bootstrap/ .

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

I have followed the example from the link above.

我已经按照上面的链接示例。

This is my data I want to send from my controller:

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

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;
      }
    }
  });
};

And this is my modal controller:

这是我的模态控制器:

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

  console.log(selectedProduct);

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

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

Problem is i can't access the "selected product" in my Modal controller. I know the reason is something to do width asyncrnous call and it can only be access from the GUI. But how do I solve this issue? How do i send the "$scope.selectedProduct" to my ModalInstanceCtrl?

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

1 个解决方案

#1


41  

You can try something like

你可以尝试类似的东西

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

Basically your $modal can take a promise, so why not use it. Now the object should be available on the controller when the promise gets resolved. The ModalInstanceCtrl should be

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

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

since you are resolving the items property not the selectedProduct property.

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

#1


41  

You can try something like

你可以尝试类似的东西

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

Basically your $modal can take a promise, so why not use it. Now the object should be available on the controller when the promise gets resolved. The ModalInstanceCtrl should be

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

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

since you are resolving the items property not the selectedProduct property.

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