[英]AngularJS: Modify Bootstrap Modal after loading content
I want to use this pretty image cropper in my AngularJS project. 我想在我的AngularJS项目中使用这个漂亮的图像裁剪器 。 When I use it the "normal" way, that is on a plain page, everything works fine. 当我以“正常”方式使用它时,即在普通页面上,一切正常。 The problem is that I want to put it on to the Bootstrap modal dialog. 问题是我想把它放在Bootstrap模态对话框中。
In order to initialize the cropper, I am supposed to execute this snippet: 为了初始化cropper,我应该执行这个片段:
$(".cropper").cropper({
aspectRatio : 1.618,
done : function(data) {
console.log(data);
}
});
But it has to be executed after the DOM of the modal is loaded. 但它必须在加载模态的DOM 之后执行。 So I have tried to run it like this: 所以我试图像这样运行它:
$scope.onCropOpen = function() {
var modalInstance = $modal.open({
templateUrl : 'cropModal.html',
controller : CropModalInstanceCtrl
});
modalInstance.opened.then(function() {
$(".cropper").cropper({
aspectRatio : 1.618,
done : function(data) {
console.log(data);
}
});
});
};
Unfortunately, the cropper still is not being initialized. 不幸的是,裁剪器仍然没有被初始化。 I know that it should work after calling it when the modal is loaded, because I ran it manually from the browser console (with the modal dialog open) and then the cropper "magically" initialized. 我知道在加载模态后调用它应该可以工作,因为我从浏览器控制台手动运行它(打开模态对话框),然后“魔术”初始化了剪切器。
Here is a very simple directive that can show you the direction. 这是一个非常简单的指令,可以向您显示方向。 Instead of defining element with class ".cropper", create element "image-cropper" (since directive name is "imageCropper"): 而不是使用类“.cropper”定义元素,创建元素“image-cropper”(因为指令名称是“imageCropper”):
<image-cropper></image-cropper>
This is a pretty general snippet that should work in similar way for any jquery plugin. 这是一个非常通用的代码片段,对于任何jquery插件都应该以类似的方式工作。
*Don't forget to change module name from "myApp", to the name of your application module... *不要忘记将模块名称从“myApp”更改为应用程序模块的名称...
angular.module("myApp").directive('imageCropper', function ($parse) {
return {
restrict: "E",
replace: true,
compile: function (element, attrs) {
var modelAccessor = $parse(attrs.ngModel);
var html = "<img></img>";
var newElem = $(html);
newElem.attr("src", attrs.src);
element.replaceWith(newElem);
return function (scope, element, attrs, controller) {
element.cropper({
aspectRatio : 1.618,
done : function(data) {
console.log(data);
}
});
};
}
};
});
Try to put the jquery code inside CropModalInstanceCtrl function: 尝试将jquery代码放在CropModalInstanceCtrl函数中:
var CropModalInstanceCtrl = function ($scope, $modalInstance) {
$(".cropper").cropper({
aspectRatio : 1.618,
done : function(data) {
console.log(data);
}
});
}
Vitali's solution is done the proper way and definitely should be used. 维塔利的解决方案是以正确的方式完成的,绝对应该使用。 But if someone still needs a brute method, this worked for me as well: 但如果有人仍然需要一个粗暴的方法,这对我也有用:
modalInstance.opened.then(function() {
$timeout(function() {
$(".cropper").cropper({
aspectRatio : 1.0,
done : function(data) {
console.log(data);
}
});
});
});
Generally, this type of solutions should be avoided because timeouts might be treacherous. 通常,应避免使用此类解决方案,因为超时可能是危险的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.