如何在加载角度函数后运行jQuery代码
How to run a jQuery code after an angular function was loaded?
基本上,我通过angular上传一个图像,上传ng文件,然后我想显示它并初始化一个名为Cropper的jQuery插件。
如何从这个代码中运行jQuery代码?它的唯一工作方式是在上传之前加载$scope.loadedImage
。
HTML:
<img id="cropImage" ng-src="/{{loadedImage}}" />
角度
$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);
});
}
我会编写一个自己的指令,在加载图像时观察src属性并初始化cropper。
在我看来,image.onload只会触发一次,因此您必须删除并放置新图像,但一开始不删除即可尝试:)
编辑:正如Kevin 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 );
}
} );
} );
} );
} );
}
}
} ] );
对于这种情况,您可以使用setTimeout并在稍有延迟后执行jquery代码。因为angular在视图中绑定数据需要一点(非常少)时间。所以你需要一点延迟。
此外,我还写了一篇博客文章,其中我给出了一个通用的解决方案,可以在ng-reeat完成绑定所有数据后调用回调函数。你也可以试试。请在此处查看此帖子。如果你有任何困惑,请告诉我。谢谢
相关文章:
- 对动态创建的元素运行jQuery.ech()
- 如何在加载角度函数后运行jQuery代码
- 在AJAX jQuery加载()之后运行jQuery代码
- 如何仅在设备基于 iOS 时运行 jQuery 代码
- 使用 .on 函数运行 jQuery 声明函数
- html代码没有运行jquery问题
- 仅在尚未运行jQuery UI的情况下运行动画
- 在运行jquery之前从5开始倒计时
- 在不重新运行jquery验证的情况下检查表单是否有效
- 在Angular App中运行jQuery函数
- 一旦设置了PHP查询字符串,如何运行jQuery脚本
- 如何在Ember中更改模板时运行JQuery代码
- 如果URL包含字符串don't运行jQuery函数
- 在php之前运行jQuery脚本
- 无法动态地将 jquery 添加到头部并运行 jquery 代码
- 在 HTML 中运行 jquery 脚本
- 如何在控制器外部和视图内在 AngularJs 中运行 jQuery
- 在具有相同类的每个表上运行 jquery
- 在 Angular 完成渲染后运行 JQuery 代码
- 如何选择当前 DIV ID 以使用该 ID 运行 Jquery