dongzhang4301
dongzhang4301
2017-04-07 16:23

如何在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 dqwh1208 4年前

    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>
    
    点赞 评论 复制链接分享

为你推荐