【问题标题】:crop doesn't work inside bootstrap modal裁剪在引导模式中不起作用
【发布时间】:2017-05-11 16:32:33
【问题描述】:

我正在使用 ngimgcrop 裁剪图像,它可以正常工作,但我尝试在 uibmodal 中显示图像,但它不起作用。 我尝试了一些解决方案(使用 ng-init ..),但没有一个对我有用。 在控制台中,图像是空的。

这是我的控制器:

 var app = angular.module('app', ['ngImgCrop', 'ui.bootstrap']);

    app.controller('Ctrl',  ['$scope',
        '$rootScope',
        '$uibModal',
        '$log',
        function($scope,
                 $rootScope,
                 $uibModal,
                 $log) 
        {



            $scope.animationsEnabled = true;
            $scope.open = function (size) {
                // alert('open mthod is called');
                $scope.test = 5;
                var modalInstance = $uibModal.open({
                    animation: true,
                    ariaLabelledBy: 'modal-title',
                    ariaDescribedBy: 'modal-body',
                    templateUrl: "imageModal.html",
                    controller: 'Ctrl',
                    controllerAs: '$ctrl',
                    size: size

                });

                modalInstance.result.then(function (selectedItem) {
                    $log.info('selected value:'+selectedItem);
                }, function () {
                    $log.info('Modal dismissed at: ' + new Date());
                });
            };

            $scope.size='small';
            $scope.type='circle';
            $scope.imageDataURI='';
            $scope.resImageDataURI='';
            $scope.resImgFormat='image/png';
            $scope.resImgQuality=1;
            $scope.selMinSize=100;
            $scope.resImgSize=200;
            $scope.test=225;
            //$scope.aspectRatio=1.2;
            $scope.onChange=function($dataURI) {
                console.log('onChange fired');
            };
            $scope.onLoadBegin=function() {
                console.log('onLoadBegin fired');
            };
            $scope.onLoadDone=function() {
                console.log('onLoadDone fired');
            };
            $scope.onLoadError=function() {
                console.log('onLoadError fired');
            };

        $scope.uploadFile = function(file) {
            if (file) {
                // ng-img-crop
                var imageReader = new FileReader();
                imageReader.onload = function(image) {
                    $scope.$apply(function($scope) {
                        $scope.imageDataURI= image.target.result;
                    });
                };
                imageReader.readAsDataURL(file);
                 $scope.open();
            }
        };

            console.log(' my image', $scope.imageDataURI);

            $scope.$watch('resImageDataURI',function(){
            console.log('Res image', $scope.resImageDataURI);
                    });
            }]);

imagemodal.html

<div ng-if="enableCrop=true" class="cropArea"   ng-class="{'big':size=='big', 'medium':size=='medium', 'small':size=='small'}">

<img-crop image="imageDataURI"
          result-image="$parent.resImageDataURI"

          change-on-fly="changeOnFly"
          area-type="{{type}}"
          area-min-size="selMinSize"
          result-image-format="{{resImgFormat}}"
          result-image-quality="resImgQuality"
          result-image-size="resImgSize"
          on-change="onChange($dataURI)"
          on-load-begin="onLoadBegin()"
          on-load-done="onLoadDone()"
          on-load-error="onLoadError()"
></img-crop>
<!--aspect-ratio="aspectRatio"-->

演示: demo

【问题讨论】:

    标签: javascript angularjs html angular-ui-bootstrap ng-img-crop


    【解决方案1】:

    如果您想显示从控制器到模态的裁剪图像,请在此参数中使用 resolve 输入 img-crop 的变量 result-image。就像:

    var modalInstance = $uibModal.open({
                    animation: true,
                    ariaLabelledBy: 'modal-title',
                    ariaDescribedBy: 'modal-body',
                    templateUrl: "imageModal.html",
                    controller: 'Ctrl',
                    controllerAs: '$ctrl',
                    size: size,
                    resolve:{
                        croppedImg:$scope.imageCrop
                    }
    
                });
    

    当你的 imageCrop 看起来像

    <img-crop image="imageDataURI"
          result-image="imageCrop"
    

    在模态控制器中注入croppedImg 作为普通的提供者/服务/工厂就像:

    function Ctrl($scope, croppedImg, ..another, ..etc)
    

    通过这种方式,在控制器中,您可以在模态控制器中获得裁剪的图像。那么只有

    $scope.newImg = croppedImg
    

    并在模态中显示为&lt;img src="{{newImg}}"&gt;

    希望你能理解。

    【讨论】:

    • TL;TR -> 将裁剪后的字符串从控制器注入到模态控制器
    • 你能举个例子吗?
    • 您想将裁剪后的图像显示为模态,还是以模态显示原始图像?将您的代码放在 plunker、jsbin 等上
    • 是的,我想显示原始图像 [plnkr.co/edit/sluRdT9y1gJRWZItgw3H?p=preview]
    • 首先 - 您在正文和模态中使用相同的控制器。它可以工作,但它不是一个好的做法。我创建了新的控制器,它只有两个注入,范围和图像。最后它工作。看:plnkr.co/edit/ql9M61XJMZvASn3t2qYq?p=preview 我做了同样的事情,我在回答中写道。
    猜你喜欢
    • 2015-03-02
    • 1970-01-01
    • 1970-01-01
    • 2014-01-17
    • 2016-11-20
    • 1970-01-01
    相关资源
    最近更新 更多