简体   繁体   English

将数据传递给角度ui模态(灯箱效果)

[英]Pass data to angular ui modal (lightbox effect)

Problem: I want to create a modal lightbox using the Angular UI Bootstrap modal 问题:我想使用Angular UI Bootstrap模式创建一个模态灯箱

Details: I have built a photo grid using ng-repeat. 细节:我使用ng-repeat构建了一个照片网格。 Each repeated photo opens the modal using the open() method. 每张重复的照片都使用open()方法打开模态。 I'm struggling with how to pass the scope of the clicked item to the modal so I can grab the image url to display. 我正在努力将如何将点击的项目的范围传递给模态,以便我可以抓取图像网址来显示。 I've implemented the scope parameter on the modal, which gives me access to the parent; 我已经在模态上实现了scope参数,这使我可以访问父模式; however the parent is the parent scope of the clicked item and contains the whole array of all images in the grid. 但是,父项是所单击项的父作用域,并包含网格中所有图像的整个数组。 I need to figure out how to tell (programmatically) which index has been clicked, or send just the child scope to the modal. 我需要弄清楚如何(以编程方式)告诉单击了哪个索引,或者只将子范围发送到模态。 I'm a newbie... if I'm missing something or there's a better way to approach this, any help is welcome. 我是新手......如果我错过了什么或者有更好的方法来解决这个问题,欢迎任何帮助。

My HTML: 我的HTML:

<section ng-controller="ModalDemoCtrl">
  <div ng-repeat="photo in photos.data">

    <img src="{{photo.source}}" class="thumbnail img-responsive" ng-click="open()">

  </div>
</section>

Instance and controller: 实例和控制器:

app.controller('ModalDemoCtrl', function ($scope, $modal, $log) {

  $scope.items = ['item1', 'item2', 'item3'];

  $scope.open = function (scope) {

    var modalInstance = $modal.open({
      templateUrl: 'myModalContent.html',
      scope: $scope,
      controller: ModalInstanceCtrl,
      resolve: {
        items: function () {
          return $scope.items;
        },
// this returns as undefined
        photo: function(){
            return $scope.photo;
        }
      }
    });


    modalInstance.result.then(function (selectedItem) {
      $scope.selected = selectedItem;
    }, function () {
      $log.info('Modal dismissed at: ' + new Date());
    });
  };
});


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



  $scope.items = items;
  $scope.photo = photo;
  $scope.selected = {
    item: $scope.items[0]
  };


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


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

};

This is basically how scope looks. 这基本上是范围的外观。 The item index I need is buried deep and I need to know (programmatically) which one was clicked. 我需要的项目索引深埋,我需要(以编程方式)知道哪一个被点击了。 I need the source off Index[0] 我需要源关闭索引[0]

$scope
--$parent
---$parent
----$photos
-----$$v
------data
-------0
--------Source
-------1
-------2
-------3
-------4
-------5
-------6
-------7
-------8

You could just do something like this. 你可以做这样的事情。

HTML HTML

<img src="{{photo.source}}" class="thumbnail img-responsive" ng-click="open(photo)">

Javascript 使用Javascript

$scope.open = function (photo) {

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

      photo: function(){
          return photo;
      }
    }
});

I tried to post this as a comment but apparently it's too long. 我试图将此作为评论发布,但显然它太长了。 So, I will post it as an answer even though the correct answers have been given. 因此,即使给出了正确的答案,我也会将其作为答案发布。

If your definition was 如果你的定义是

$scope.open = function (xyz) {...

then your resolve would be 那么你的决心就是

...photo: function(){ return xyz;}

You just got confused because you had used string name 'photo' as your function parameter. 您只是因为使用了字符串名称“photo”作为函数参数而感到困惑。 It has nothing to do with the scope. 它与范围无关。 Also in your resolve definition, you could have called it anything instead of photo 同样在您的解析定义中,您可以将其称为任何内容而不是照片

...abc: function() {return xyz} 

and then use abc in your 然后在你的。中使用abc

ModelInstanceCtrl(... , abc)

Again, there is no link to scope here. 同样,这里没有范围链接。 You are just passing along a value from 你只是传递一个值

open(photo) to function (xyz) to ModalInstanceCtrl (... , abc)

Within the controller you can set it to anything you would like 在控制器中,您可以将其设置为您想要的任何内容

$scope.xxx = abc;

photo actually does not exist in the main scope since ng-repeat creates a local scope within the loop. 由于ng-repeat在循环内创建了一个局部作用域,因此主要作用域中实际上不存在。 photo is only visible within the loop and that's why you have to pass along to the controller through the function open() parameter. 照片只在循环中可见,这就是你必须通过函数open()参数传递给控制器​​的原因。 I am new to Angular and keeping track of scope lives has been challenging. 我是Angular的新手,跟踪范围生活一直充满挑战。 Experts out there, please correct me if I am wrong. 那里的专家,如果我错了,请纠正我。

Can't you just pass the photo into open? 你不能把照片打开吗? Ie ng-click="open(photo)" 即点击=“打开(照片)”

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

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