dongzhang4301 2017-04-07 16:23
浏览 69
已采纳

如何在Angular中移动图像(blob)?

I have an image gallery displayed on my page. I need to implement a modal for whenever the user clicks on the images. In the modal I need to show the full size of the selected image. Here is the problem: I have already made the modal work, but when I click on any of the gallery images, the modal shows all of them together in a single modal. I need the modal to only show the one that the user clicked on.

Please note that my webpage is based on AngularJS and PHP. I used ngModal for this modal and that I'm new to using Angular (basically I know nothing, I'm learning), so please be patient with me. Here is my code:

app.js

readApp.controller('newsGallery', function($scope) {
    $scope.myData = {
      modalShown: false,
    }
    $scope.logClose = function() {
      console.log('close!');
    };
    $scope.toggleModal = function() {
      $scope.myData.modalShown = !$scope.myData.modalShown;
    };
});

HTML

<div ng-controller='newsGallery'>
   <modal-dialog show='myData.modalShown' width='75%' height='80%' on-close='logClose()'>

    <div ng-repeat = "i in idsBlobs" >
      <img src="php/visualizar_archivo.php?id={{i.id}}">
         </div>
   </modal-dialog>

   <div class="row" style="display:flex; flex-wrap: wrap;">
      <div class = "col-md-4" ng-repeat = "i in idsBlobs" >
        <div class="news-image" align="center">
           <img src="php/visualizar_archivo.php?id={{i.id}}" class = "img-responsive img-rounded" ng-click='toggleModal();'>
        </div>
      </div>
   </div>                 
 </div>
  • 写回答

1条回答 默认 最新

  • dqwh1208 2017-04-07 17:33
    关注

    One way to have the image that the user clicked on shown in the modal is to introduce a scope variable e.g. $scope.selectedImage. Next, in the function toggleModal(), accept an argument for the image and set that scope variable to that argument.

    $scope.toggleModal = function(image) {
        $scope.myData.modalShown = !$scope.myData.modalShown;
        $scope.selectedImage = image;
    };
    

    Next update the call to that function in the ng-click handler:

    <img src="php/visualizar_archivo.php?id={{i.id}}" ng-click='toggleModal(i);' class = "img-responsive img-rounded">
    

    Then in the modal markup, show that selected image.

    <modal-dialog show='myData.modalShown' width='75%' height='80%' on-close='logClose()'>
      <img src="php/visualizar_archivo.php?id={{selectedImage.id}}">
    </modal-dialog>
    

    That way the modal will only show the image that the user clicked on, instead of all images in the list.

    See a demonstration of this below.

    readApp = angular.module('readApp', ["ngModal"]);
    readApp.controller('newsGallery', function($scope) {
      $scope.idsBlobs = [{
          "id": 'MA',
          "src": "http://www.animatedimages.org/data/media/96/animated-lighthouse-image-0032.gif"
        },
        {
          "id": "MU",
          "src": "http://icons.iconarchive.com/icons/aha-soft/large-home/128/Museum-icon.png"
        }
      ];
    
      $scope.myData = {
        modalShown: false
      }
      $scope.logClose = function() {
        console.log('close!');
      };
      $scope.toggleModal = function(image) {
        $scope.myData.modalShown = !$scope.myData.modalShown;
        $scope.selectedImage = image;
      };
    });
    .img-thumb {
      height: 48px;
      width: 48px;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
    <script src="//elliott.andrewz.org/cdn/ng-modal.min.js"></script>
    <link href="//elliott.andrewz.org/cdn/ng-modal.css" rel="stylesheet" />
    <div ng-app="readApp" ng-controller="newsGallery">
      <modal-dialog show="myData.modalShown" width="75%" height="80%" on-close="logClose()">
        <img src="{{ selectedImage.src }}" />
      </modal-dialog>
      <div class="row" style="display:flex; flex-wrap: wrap;">
        <div class="col-md-4" ng-repeat="i in idsBlobs">
          <div class="news-image" align="center">
            <img src="{{ i.src }}" class="img-responsive img-rounded img-thumb" ng-click="toggleModal(i);" />
          </div>
        </div>
      </div>
    </div>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)