2016-02-28 88 views
0

我試圖將一個Ng重複傳遞給我的$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"> 
+0

如果你想訪問html中的圖像,不應該是vm.image = image? –

+0

@KarthikRP不,這並不能解決問題。 –

回答

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 
     } 
    }); 
}; 

這應該工作。

+0

好的我在我的控制器中試過,但我仍然無法傳遞圖像。我不再有控制檯錯誤,這是一個進步。我的另一個問題是我的按鈕'取消()'不起作用。 'clickOutsideToClose'正在工作。 –

+0

您正在使用controllerAs方式,所以最好將方法和圖像變量綁定到控制器('this')。我將編輯我的答案,並在模態的html中添加所需的關閉按鈕和ng-click。 – 32teeths

+0

希望工程@AustinPerez – 32teeths