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 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来