简体   繁体   English

如何在加载角度函数后运行 jQuery 代码?

[英]How to run a jQuery code after an angular function was loaded?

Basically I upload an image through angular with ng-file-upload and then I want to display it and initialize a jQuery plugin called Cropper.基本上,我使用 ng-file-upload 通过 angular 上传图像,然后我想显示它并初始化一个名为 Cropper 的 jQuery 插件。

How can I run the jQuery code from this one?我怎样才能从这个运行 jQuery 代码? The only way it works is when I have the $scope.loadedImage loaded before the upload.它工作的唯一方法是在上传之前加载$scope.loadedImage

HTML: HTML:

<img id="cropImage" ng-src="/{{loadedImage}}" />

Angular

  $scope.upload = function (dataUrl) {
    Upload.upload({
        url: '/photocalc/upload/file',
        method: 'POST',
        file: dataUrl,
    }).then(function (response) {
        $timeout(function () {
            $scope.result = response.status;
            $scope.loadedImage = response.data;

            jQuery('#cropImage').cropper({
              viewMode: 0,
              zoomable: false,
              dragMode: 'crop',
              guides: true,
              highlight: true,
              cropBoxMovable: true,
              cropBoxResizable: true,
              crop: function(e) {
                // Output the result data for cropping image.
                console.log(e.x);
                console.log(e.y);
                console.log(e.width);
                console.log(e.height);
                console.log(e.rotate);
                console.log(e.scaleX);
                console.log(e.scaleY);
              }
            });
        });
    }, function (response) {
        if (response.status > 0) $scope.errorMsg = response.status
            + ': ' + response.data;
    }, function (evt) {
        $scope.progress = parseInt(100.0 * evt.loaded / evt.total);
    });
}

I would write an own directive which will observe the src attribute and init the cropper when the image loaded.我会编写一个自己的指令,它将观察 src 属性并在加载图像时初始化裁剪器。

In my mind the image.onload will only fire once so you would have to remove and place a new image but try it without removing at first :) 在我看来,image.onload 只会触发一次,因此您必须删除并放置一个新图像,但首先尝试不删除:)
edit: as Kevin B said this thought was wrong编辑:正如凯文 B 所说,这个想法是错误的

angular.module( "$name$" ).directive( "imageCropper", [ function () {
    return {
        restrict: "A",
        link    : function ( $scope, $el, $attr ) {
            $attr.$observe( "src", function ( src ) {
                $el.on( "load", function () {
                    $timeout( function () {
                        $scope.result      = response.status;
                        $scope.loadedImage = response.data;

                        jQuery( '#cropImage' ).cropper( {
                            viewMode        : 0,
                            zoomable        : false,
                            dragMode        : 'crop',
                            guides          : true,
                            highlight       : true,
                            cropBoxMovable  : true,
                            cropBoxResizable: true,
                            crop            : function ( e ) {
                                // Output the result data for cropping image.
                                console.log( e.x );
                                console.log( e.y );
                                console.log( e.width );
                                console.log( e.height );
                                console.log( e.rotate );
                                console.log( e.scaleX );
                                console.log( e.scaleY );
                            }
                        } );
                    } );
                } );
            } );
        }
    }
} ] );

For this kind of situation you can use setTimeout and execute your jquery code after a little delay.对于这种情况,您可以使用 setTimeout 并在稍微延迟后执行您的 jquery 代码。 Because it took a little bit(a very little) of time for angular to bind data in the view.因为 angular 在视图中绑定数据需要一点(很少)时间。 So you need a little delay.所以你需要一点延迟。

Also I've written a blog post where I've given a generic solution to call a callback function after ng-repeat finish binding all its data.我还写了一篇博客文章,其中我给出了一个通用解决方案,用于在 ng-repeat 完成绑定其所有数据后调用回调函数。 You can try that as well.你也可以试试。 Check this post here. 在此处查看此帖子。 Let me know if you have any confusion.如果您有任何困惑,请告诉我。 Thanks.谢谢。

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

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