dongzhang4301 2017-04-07 08: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 09: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 centos7.6进不去系统,卡在数字7界面
  • ¥15 Tensorflow采用interpreter.allocate_tensors()分配内存出现ValueError: vector too long报错
  • ¥15 使用CGenFF在线生成血红素辅基拓扑结构遇到问题
  • ¥15 在fragment使用okhttp同步上传文件,能不能在fragment销毁后还可以继续上传文件?
  • ¥20 matlab代码实现可达矩阵形成骨骼矩阵
  • ¥15 关于地板的木纹和图库中的匹配的
  • ¥30 机器学习预测疾病模型流程疑问
  • ¥50 2048Python实现
  • ¥15 使用ads进行低噪放仿真没有结果且不报错
  • ¥15 关于#python#的问题:有偿求一个千寻框架找书机器人插件
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部