简体   繁体   English

在我的md对话框中无法访问Angular-Material,AngularJS,TypeScript,Javascript,ng-controller

[英]Angular-Material , AngularJS , TypeScript , Javascript , ng-controller not accessible within my md-dialog

I have a problem in my code, I'm trying to call on my users with a ng-controller in (declared on index like this ' ng-controller="mainController as vm" ') and I can use this in my code to access the user and to get his email and images. 我的代码中有一个问题,我正在尝试使用ng-controller调用我的用户(在索引上声明'ng-controller =“mainController as vm”')我可以在我的代码中使用它访问用户并获取他的电子邮件和图像。 But now I want to be able to open a md-dialog when you click my image , so the image can be enlarged in the dialog, my dialog opens but I cant access my controller (vm) to say what image he has to show. 但是现在我希望能够在你点击我的图像时打开一个md对话框,这样图像就可以在对话框中放大,我的对话框打开但是我无法访问我的控制器(vm)来说明他要显示的图像。

This opens the image in my content page: 这将打开我的内容页面中的图像:

<img src="{{vm.selected.item4}}" />

works perfectly btw 顺便说一下

and this is how I open the dialog: 这就是我打开对话框的方式:

<md-button ng-click="vm.showDialog($event)" >
            <img src="{{vm.selected.item1}}"/>
</md-button>

My dialog opens but it won't show the image... Has anyone got an idea of how to resolve this problem? 我的对话框打开但它不会显示图像...有没有人知道如何解决这个问题?

(if my topic or question isn't formed correctly I'm sorry in advance , I'm new to this so correct me if something isn't like it's suppose to...) (如果我的主题或问题的格式不正确,请先对不起,我对此很陌生,因此,如果某些内容不符合我的要求,请纠正我。)

There are several ways to pass a value from parent scope to your dialog. 有几种方法可以将值从父作用域传递到对话框。 Here are 3 example usages. 以下是3个示例用法。 You can play with them by using this codepen . 你可以使用这个codepen来玩它们。

.controller('AppCtrl', function ($scope, $mdDialog) {

  $scope.imageUrl = "https://upload.wikimedia.org/wikipedia/commons/1/18/Bradypus.jpg";

    // Show the image in the dialog by using parent scope directly in the template.
  $scope.showDialog = function(ev) {
    $mdDialog.show({
      template: '<img src="' + $scope.imageUrl + '"></img>',
      parent: angular.element(document.querySelector('#popupContainer')),
      targetEvent: ev,
      clickOutsideToClose: true,
      ariaLabel: 'Dialog 1',
    });
  }

  // Get the image from the function call and use directly in the template
  $scope.showDialog2 = function(ev, image) {
    $mdDialog.show({
      template: '<img ng-src="' + image + '"></img>',
      parent: angular.element(document.querySelector('#popupContainer')),
      targetEvent: ev,
      clickOutsideToClose: true,
      ariaLabel: 'Dialog 2'
    });
  }

  // Show the image by defining locals. This is the most elegant way, 
  // IMO. You can make any value available by defining locals. After 
  // defining locals, you can use the defined locals in your dialog 
  // controller. After you get the locals from your controller, you can 
  // easily use them in your dialog's template.
  $scope.showDialog3 = function(ev) {
    $mdDialog.show({
      template: '<img ng-src="{{image}}"></img>',
      parent: angular.element(document.querySelector('#popupContainer')),
      targetEvent: ev,
      clickOutsideToClose: true,
      ariaLabel: 'Dialog 3',
      locals: {image: $scope.imageUrl},
      controller: function($scope, $log, image) {
        $scope.image = image;
      }
    });
  }

});

HTML: HTML:

<div ng-app="MyApp">
  <div ng-controller="AppCtrl">
    <md-content id="popupContainer" style="height: 500px;">

      <md-button class="md-raised md-primary" ng-click="showDialog($event)">
        Open Dialog
      </md-button>
      <md-button class="md-raised md-primary" ng-click="showDialog2($event, imageUrl)">
        Open Dialog 2
      </md-button>
      <md-button class="md-raised md-primary" ng-click="showDialog3($event)">
        Open Dialog 3
      </md-button>

    </md-content>
  </div>
</div>

You can also check the examples from the docs . 您还可以查看文档中的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM