简体   繁体   English

将参数从控制器传递到模式指令

[英]Passing parameters from a controller to a modal directive

I am trying to popup different modals based on which area of an imagemap is clicked. 我试图根据单击图像映射的区域弹出不同的模态。 Each area of the imagemap is tied to information from a JSON file and I need to be able to refer to that information when creating the unique modal for each room. 图像映射的每个区域都与JSON文件中的信息相关联,在为每个房间创建唯一模态时,我需要能够引用该信息。 To do this i need the scope that my modal directive is under to access information in the scope of the main controller. 为此,我需要我的模式指令所在的作用域来访问主控制器范围内的信息。 I know there are similar questions posted and believe me I have tried to implement any solutions I have found but I cant get anything working. 我知道也张贴了类似的问题,并且相信我已经尝试实现我发现的所有解决方案,但是我什么也做不了。

here is the app page including the controller and modal directive with comments mentioning things ive tried: 这是应用程序页面,其中包括控制器和模态指令,并附带注释,提及我尝试过的事情:

var myApp = angular.module('myApp', []);

myApp.controller('MainCtrl', ['$scope', '$http', function ($scope, $http) {
  $http.get('js/master.json').success(function(data) {
           $scope.rooms = data;
       });
$scope.showModal = false;
$scope.toggleModal = function(roomId){
    $scope.showModal = !$scope.showModal;
    $scope.roomId = roomId;
};
 $scope.load = function() {
      $('img[usemap]').maphilight();
    }
 }]);



myApp.directive('modal', function () {
    return {
      template: '<div class="modal fade">' + 
      '<div class="modal-dialog">' + 
        '<div class="modal-content">' + 
          '<div class="modal-header">' + 
            '<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>' + 
            '<h1 class="modal-title">{{ title }}</h1>' + 
          '</div>' + 
          '<div class="modal-body" ng-transclude></div>' + 
        '</div>' + 
      '</div>' + 
    '</div>',
  restrict: 'E',
  transclude: true,
  replace:true,
  scope: true, //Ive tried changing "true" to "$scope"
  link: function postLink(scope, element, attrs) {
    scope.title = attrs.title;
    scope.rooms = scope.$parent.rooms; //doesnt work
    scope.roomId = scope.$parent.roomId; //doesn't work

<!--scope.$watch(attrs.rooms, function(value){
      scope.rooms = value;
    }); --> //tried something like this but doesnt work

    scope.$watch(attrs.visible, function(value){
      if(value == true)
        $(element).modal('show');
      else
        $(element).modal('hide');
    });

    $(element).on('shown.bs.modal', function(){
      scope.$apply(function(){
        scope.$parent[attrs.visible] = true;
      });
    });

    $(element).on('hidden.bs.modal', function(){
      scope.$apply(function(){
        scope.$parent[attrs.visible] = false;
      });
    });
  }
};
});

and here is the html that uses the directive and needs access to "rooms" and "roomId" from previous scope 这是使用指令的html,需要从上一个作用域访问“ rooms”和“ roomId”

<img ng-src="images/housediagram.jpg" alt="Photo of thing" class="map" usemap="#houseMap" data-ng-init="load()">

  <map name="houseMap" id="map">
    <area ng-repeat="room in rooms" name="{{room.room}}" id="{{room.id}}" shape="poly" 
coords="{{room.coords}}" ng-click="toggleModal(roomId)"/>
  </map>

 <modal title="{{rooms[roomId].room}}" visible="showModal"> //title doesnt display anything
<ul>
    <li>
        <h4>Content dependent on data in 'room'</h4>
    </li>
</ul>
</modal>

here is a fiddle that doesnt work but it has the code all in one place http://jsfiddle.net/nateholmes3/kh6vsd1b/2/ 这是一个小提琴,不起作用,但是它的代码全部放在一个地方http://jsfiddle.net/nateholmes3/kh6vsd1b/2/

And to be clear the modal works completely fine, it just needs to access those variables. 需要明确的是,模态工作完全正常,只需要访问这些变量即可。 thanks a ton in advance 在此先感谢一吨

Take a look at how angular bootstrap https://angular-ui.github.io/bootstrap/ implements their modal. 看看角度引导程序https://angular-ui.github.io/bootstrap/如何实现其模态。 When using it the scope of the controller that creates the modal is still accessible. 使用它时,仍然可以访问创建模态的控制器的范围。

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

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