简体   繁体   English

如何使用角度ui模态将数据传递到控制器?

[英]How to pass data to the controller using angular ui modal?

When user choose the image file, I am try to open modal window to crop the image, but I cant pass it to the modal window controller and vice-versa. 当用户选择图像文件时,我尝试打开模式窗口以裁剪图像,但是无法将其传递给模式窗口控制器,反之亦然。 This is my code: 这是我的代码:

<!doctype html>
<html ng-app="ui.bootstrap.demo">
  <head>
    <script src="http://code.jquery.com/jquery-2.1.0.min.js"></script>

    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.js"></script>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular-animate.js"></script>
    <script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-2.0.0.js"></script>
    <script src="example.js"></script>

<link href="ng-img-crop.css" rel="stylesheet" type="text/css">
<script src="ng-img-crop.js"></script>

    <link href="//netdna.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
  </head>
  <body>

<div ng-controller="ModalDemoCtrl">
                        <input id="coolButton" name="avatar" type="file" accept="image/*" class="form-control" placeholder="">

    <div class="hidden">
    <div id="myModalContent">
              <div class="modal-header">
            <h3 class="modal-title">I'm a modal!</h3>
        </div>
        <div class="modal-body">

            <div class="cropArea">
              <style>
                .cropArea {
                  background: #E4E4E4;
                  overflow: hidden;
                  width:300px;
                  height:350px;
                }
              </style>
              <img-crop image="myImage" result-image="myCroppedImage"></img-crop>
            </div>
            <div>Cropped Image:</div>
            <div><img ng-src="{{myCroppedImage}}"/></div>

            <ul>
                <li ng-repeat="item in items">
                    <a href="#" ng-click="$event.preventDefault(); selected.item = item">{{ item }}</a>
                </li>
            </ul>
            Selected: <b>{{ selected.item }}</b>
        </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>
    </div>
    </div>
    <button type="button" class="btn btn-default" ng-click="open()">Open me!</button>
    <!--button type="button" class="btn btn-default" ng-click="open('lg')">Large modal</button>
    <button type="button" class="btn btn-default" ng-click="open('sm')">Small modal</button>
    <button type="button" class="btn btn-default" ng-click="toggleAnimation()">Toggle Animation ({{ animationsEnabled }})</button-->
    <div ng-show="selected">Selection from a modal: {{ selected }}</div>

</div>
  </body>
</html>

Here is the demo. 是演示。

Just pass the $scope to the scope field when open the modal. 打开模式时,只需将$ scope传递到scope字段即可。 this will let the modal use $scope as its parent scope. 这将使模式使用$ scope作为其父范围。 If the scope is not set, it will use $rootScope as its parent by default. 如果未设置范围,则默认情况下它将使用$ rootScope作为其父级。

var modalInstance = $uibModal.open({
      animation: $scope.animationsEnabled,
      //templateUrl: 'myModalContent.html',
        template: template,
      controller: 'ModalInstanceCtrl',
      size: size,
      scope:$scope, // set the scope 
      resolve: {
        items: function () {
          return $scope.items;
        }
      }
    });

Or if u do want to make ur modal more independently. 或者,如果您确实想使您的模态更独立。 u need to pass the image's value in resolve just as u did to items . 您需要像传递给items一样传递图像的值来进行resolve

var modalInstance = $uibModal.open({
      animation: $scope.animationsEnabled,
      //templateUrl: 'myModalContent.html',
        template: template,
      controller: 'ModalInstanceCtrl',
      size: size,
      resolve: {
        items: function () {
          return $scope.items;
        },
        myImage:function(){
          return $scope.myImage
        } // send the image data to the modal's controller
      }
    }); 

But in this way u must call '$scope.open' after the image was readed. 但是以这种方式,您必须在读取图像后调用“ $ scope.open”。

 reader.onload = function (evt) {
          $scope.$apply(function($scope){
              $scope.myImage=evt.target.result;
              $scope.open();
          });
      };

UPDATE: 更新:

$scope.myCroppedImage = null; // looks like u need to init it first
  $scope.ok = function () {
    $uibModalInstance.close({selectedItem:$scope.selected.item,myCroppedImage:$scope.myCroppedImage});
  };

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

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