将图像传递给$ mdDialog

时间:2016-02-28 06:28:25

标签: javascript angularjs angular-material

我正在尝试将Ng-repeat传递给我的$mdDialog,但我找不到有关如何执行此操作的文档。我一直指的是This Stack,但是没有运气将图像传递给模态。

在控制台中我收到错误消息:

TypeError: Cannot read property 'element' of undefined

控制台错误的原因是什么,而不是将图像传递给模态?

HTML

<md-grid-tile 
    ng-repeat="image in imageBucket.images"
    md-rowspan="{{image.row}}"
    md-rowspan-gt-lg="{{image.rowgtlg}}"
    md-colspan="{{image.col}}"
    md-colspan-gt-lg="{{image.colgtlg}}"
    class="white" >
    <md-button 
        class="" 
        ng-click="showAdvanced($event, image)" 
        flex="100" 
        flex-gt-md="auto">
        <img 
            aria-label="kpinsonstairs" 
            class="img-responsive md-whiteframe-6dp" 
            src="{{image.src}}" 
            alt="Gallery Picture">
            <md-grid-tile-footer>
                <h3>{{image.title}}</h3>
            </md-grid-tile-footer>
    </md-button>
</md-grid-tile>

的Javascript

(function () {

  'use strict';

    angular
        .module('resonance.gallery.controllers')
        .controller('GalleryOneController', GalleryOneController);

    GalleryOneController.$inject = [
        '$scope',
        '$mdDialog',
        'ImageService'
    ];

    function GalleryOneController($scope, $mdDialog, ImageService) {

      ImageService.success(function(data) {
        $scope.imageBucket = data;
      });


      $scope.showAdvanced = function(ev, image) {

        $mdDialog.show({
          clickOutsideToClose:true,
          controller: function($mdDialog) {
            var vm = this;
            var image = {};
            var image = image;
            $scope.hide = function() {
              $mdDialog.hide();
            }
            $scope.cancel = function() {
              $mdDialog.cancel();
            };
          },
          controllerAs: 'modal',
          templateUrl: 'client/gallery/views/dialog.ng.html',
          parent: angular.element(document.body),
          targetEvent: ev
        });
      };
    }
})();

模态HTML

<img 
     class="img-responsive md-whiteframe-6dp" 
     src="{{modal.image.src}}" 
     alt="Gallery Picture">

1 个答案:

答案 0 :(得分:6)

如果您查看angular-material的文档,您会看到有一个选项可以使用locals关键字将内容解析到模式的控制器。因此,您必须将函数重写为:

$scope.showAdvanced = function(ev, image) {
    $mdDialog.show({
        clickOutsideToClose: true,
        controller: function($mdDialog, image) {
            var vm = this;
            vm.image = image;
            $scope.hide = function() {
                $mdDialog.hide();
            };
            $scope.cancel = function() {
                $mdDialog.cancel();
            };
        },
        controllerAs: 'modal',
        templateUrl: 'client/gallery/views/dialog.ng.html',
        parent: angular.element(document.body),
        targetEvent: ev,
        locals: {
            image: image
        }
    });
};

那应该有用。