无法将范围传递给角度对话框

时间:2015-01-09 17:05:09

标签: angularjs ng-dialog

我正在使用Angular显示图像列表。

单击图像时,我想显示带有图像和更多信息的对话框。

因此,在打开模态时,我需要传递范围,或者至少是图像。

注意:我正在使用ngDialog作为弹出窗口。

  application.controller('ImageController', function ImageController($scope, ImageService, ngDialog) {

    $scope.model = {
      images: [],
      loading: false
    }

    var load = function () {
      $scope.model.loading = true;
      ImageService.GetList($scope.model.pages.instagram)
        .success(function (data, status, headers, config) {
          $scope.model.images = $scope.model.images.concat(data.Images)
        })
        .error(function (data, status, headers, config) { })
        .finally(function () {
          $scope.model.loading = false
        });;
    }

    $scope.open = function (image) {
      ngDialog.open({
        template: '<p>my template {{image.Key}} </p>',
        plain: true,
        scope: $scope
      });
    };


    load();

  });

  <div data-ng-app="Application" data-ng-controller="ImageController">
    <div data-ng-repeat='image in model.images'>
    <img src="{{image.Url}}" alt="" data-ng-click="open(image)"/>
  </div>

显示图像并单击...

打开对话框

但是,不知何故,我无法访问模板内的范围。

以下作品:

    $scope.open = function (image) {
      ngDialog.open({
        template: '<p>my template' + image.Key + '</p>',
        plain: true
      });
    };

但这不起作用:

    $scope.open = function (image) {
      ngDialog.open({
        template: '<p>my template {{image.Key}} </p>',
        plain: true,
        scope: $scope
      });
    };

有谁知道为什么?

我还没有弄清楚这一点。

2 个答案:

答案 0 :(得分:10)

将您的open方法更改为以下内容..

  $scope.open = function (image) {
      var newScope = $scope.$new();
      newScope.image = image;
      ngDialog.open({
        template: '<p>my template {{image.Key}} </p>',
        plain: true,
        scope: newScope
      });
    };

这应该有用......

答案 1 :(得分:1)

对我有用的最好方法是没有范围。只需使用数据属性:

 $scope.open = function (image) {          
                  ngDialog.openConfirm({
                  template: 'views/alertTemplate.html'
                  showClose: false,
                  data: {
                    image: image,
                  }
                });  
            }

并在您的模板文件中:

<div class="alert-dialog-container">
  <div class="alert-dialog-body">
    <p ng-bind-html="ngDialogData.image"></p>
  </div>
  <div class="alert-dialog-footer">
    <button ng-click="confirm()">OK</button>
  </div>
</div>
相关问题