javascript - Angular $modal 范围 - 将对象传递给 $modal

标签 javascript jquery angularjs twitter-bootstrap

我正在尝试将 Angular 的 Bootstrap 模式传递给在 Angular Masonry Gallery 中单击的图像的 URL。我所拥有的与文档中的演示非常接近,仅做了一些更改。我知道这完全是我自己对范围的理解的问题。

我的模态 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>

在我的 Controller 中:

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>

现在,这可以很好地将 li 标签中的 items 对象 3 值返回到模态,就像原始示例一样。 id 真正喜欢传递到模型中的是 (item.src),但无论我更改什么,它似乎都永远不会传递到模型中以便我可以显示它。

最佳答案

好吧,我觉得这很奇怪,但尝试使用 item.src.toString(); 由于某种原因它不喜欢返回整数。

关于javascript - Angular $modal 范围 - 将对象传递给 $modal,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32512218/

相关文章:

javascript - onAfterRendering 在加载数据 View 之前调用,使用工厂

javascript - 通过条形码阅读器等外部设备输入输入后选择并突出显示输入

jQuery if语句改变CSS

angularjs - 如何通过手动设置缩放级别来适应 map 上的屏幕?

javascript - 添加for循环后的JS函数 'not defined'

java - 在 JavaScript 中处理来自支持 bean 的换行符

javascript - 未捕获的语法错误 : Unexpected token in JSON at position 0

angularjs - 推荐 nginx 设置为 AngularJS 作为前端,Symfony2 作为后端

javascript - 在 AngularJS 中使用 Controller 手动加载模板

javascript - dragend 和 drop 事件之间是否有定义的顺序?