2015-09-10 72 views
1

我試圖通過角度的引導模態在角砌石畫廊中點擊的圖像的網址。我所擁有的與文檔中的演示非常接近,只需進行一些更改。我知道這對我自己對範圍的理解完全是一個問題。

我的HTML模式:

<div class="modal-header"> 
     <h3 class="modal-title">I'm a modal!</h3> 
    </div> 
    <div class="modal-body"> 
     <ul> 
      <li ng-repeat="item in items"> 
       <a href="#" ng-click="$event.preventDefault(); selected.item = item">{{ item }}</a> 
      </li> 
     </ul> 
    </div> 
    <div class="modal-footer"> 
     <button class="btn btn-primary" type="button" ng-click="ok()">OK</button> 
     <button class="btn btn-warning" type="button" ng-click="cancel()">Cancel</button> 
    </div> 

在我的控制器:

angular.module('testApp').controller('GalleryCtrl', function ($scope, $modal, $log) { 
    $scope.animationsEnabled = true; 
    $scope.items = ['item1', 'item2', 'item3']; 
    // Temporary Generation of images to replace with about us images 
    function genBrick() { 
     var height = ~~(Math.random() * 500) + 100; 
     var id = ~~(Math.random() * 10000); 
     return { 
     src: 'http://placehold.it/' + width + 'x' + height + '?' + id 
     }; 
    }; 
    this.bricks = [ 
    genBrick(), 
    genBrick(), 
    genBrick(), 
    genBrick(), 
    genBrick(), 
    genBrick() 
    ]; 
    this.showImage = function(item){ 
    alert(item.src); // gives me exactly what im trying to pass to the modal 
    var modalInstance = $modal.open({ 
     animation: $scope.animationsEnabled, 
     templateUrl: 'views/modal.html', 
     scope: $scope, 
     controller: 'GalleryCtrl', 
     resolve: { 
     items: function() { 
      return $scope.items; 
     } 
     } 
    }); 

    modalInstance.result.then(function (selectedItem) { 
     $scope.selected = selectedItem; 
    }, function() { 
     $log.info('Modal dismissed at: ' + new Date()); 
    }); 

    } 
}); 

最後爲了清楚起見,這是我的磚石代碼

<div masonry> 
    <div class="masonry-brick" ng-repeat="brick in gallery.bricks"> 
     <img ng-src="{{ brick.src }}" alt="A masonry brick" ng-click="gallery.showImage(brick)"> 
    </div> 
</div> 

現在這工作正常,返回項目對象3將li標籤中的值賦給模態,就像在原始示例中那樣。什麼id真的要傳入模型是(item.src),但無論我改變它似乎永遠不會傳入,以便我可以顯示它。

+0

你試過設置,爲$範圍變量? $ scope.obj = item.src; –

回答

0

好的我覺得這很奇怪,但嘗試使用 item.src.toString(); 由於某種原因,它不喜歡返回整數。

0

經過幾個小時的工作後,我確定我的原始問題很混亂,因爲一些關鍵組件完全沒有。主要的問題是我的模式沒有控制器,我需要清理這個和$ scope的混合使用,並且我需要正確使用解決方案,但是我沒有。仍然是角度新手,這是我對模態範圍的理解,導致我誤入歧途。

什麼工作了,我gallery.js

'use strict'; 
/** 
* @ngdoc function 
* @name testApp.controller:GalleryCtrl 
* @description 
* # GalleryCtrl 
* Controller of the testApp 
*/ 
angular.module('testApp').controller('GalleryCtrl', function ($scope, $modal, $log) { 

    // Temporary Generation of random images to replace with an object later 
    function genBrick() { 
     var height = ~~(Math.random() * 500) + 100; 
     var width = 280; 
     var id = ~~(Math.random() * 10000); 
     return { 
     src: 'http://placehold.it/' + width + 'x' + height + '?' + id 
     }; 
    }; 
    //Create a dummy object full of src: urls to pull images into masonry gallery 
    $scope.bricks = [ 
    genBrick(),genBrick(),genBrick(),genBrick(), genBrick(),genBrick(),genBrick(),genBrick(),genBrick(), genBrick(), 
    genBrick() 
    ]; 
    // End Temporary Generation of random images to replace with an object later 

    $scope.showImage = function(source){ 
    $scope.imageClicked = source; 
    var modalInstance = $modal.open({ 
     animation: true, 
     templateUrl: 'views/modal.html', 
     scope: $scope, 
     controller: 'ModalInstanceCtrl', 
     resolve: { 
     imageToShow: function() { // imageToShow is what needs to be passed into the modal controller 
      return $scope.imageClicked; 
     } 
     } 
    }); 

    modalInstance.result.then(function (selectedItem) { 
     $scope.selected = selectedItem; 
    }, function() { 
     $log.info('Modal dismissed at: ' + new Date()); 
    }); 

    } 
}); 

angular.module('testApp').controller('ModalInstanceCtrl', function ($scope, $modalInstance, imageToShow) { 
    $scope.image = imageToShow; // its available here because we passed it into the function and to resolve 
    $scope.cancel = function() { 
    $modalInstance.dismiss('cancel'); 
    }; 
}); 

我modal.html模板:

 <div class="modal-header"> 
      <h3 class="modal-title">I'm a modal with the image you clicked!</h3> 
     </div> 
     <div class="modal-body"> 
      <img ng-src="{{ image }}" alt=""> 
     </div> 
     <div class="modal-footer"> 
      <button class="btn btn-primary" type="button" ng-click="cancel()">Cancel</button> 
     </div> 

又一次我看來

<div masonry> 
    <div class="masonry-brick" ng-repeat="brick in bricks"> 
     <img ng-src="{{ brick.src }}" alt="A masonry brick" ng-click="showImage(brick.src)"> 
    </div> 
</div>