Angular 中的实际图像大小
Real image size in Angular
我对 Angular 有点陌生,但我正在尝试从使用 ng-repeat 获得的图像中获取真实图像大小。为了在我的 HTML 文件中显示,我调整了这些图像的大小,但我想知道实际大小是多少,为此我制定了一个指令,但我无法让它工作。
angular.module('platForm').directive("getImageResolution", function(){
return {
restrict: 'A',
scope:{
size:"=getImageResolution"
},
link: function(scope, elem, attr) {
elem.on('load', function() {
var w = $(this).width();
var h = $(this).naturalHeight;
// console.log(w,h);
scope.size = {};
scope.size.x = w;
scope.size.y = h;
console.log(scope.size);
//check width and height and apply styling to parent here.
});
}
}
});
非常感谢任何帮助:)
编辑:
var w = $(this).width();
var h = $(this).naturalHeight;
是测试,.width() 获取调整后图像的宽度(我不想要),而 naturalHeight 不起作用。
使用
elem[0].naturalWidth
改为
我对 Angular 有点陌生,但我正在尝试从使用 ng-repeat 获得的图像中获取真实图像大小。为了在我的 HTML 文件中显示,我调整了这些图像的大小,但我想知道实际大小是多少,为此我制定了一个指令,但我无法让它工作。
angular.module('platForm').directive("getImageResolution", function(){
return {
restrict: 'A',
scope:{
size:"=getImageResolution"
},
link: function(scope, elem, attr) {
elem.on('load', function() {
var w = $(this).width();
var h = $(this).naturalHeight;
// console.log(w,h);
scope.size = {};
scope.size.x = w;
scope.size.y = h;
console.log(scope.size);
//check width and height and apply styling to parent here.
});
}
}
});
非常感谢任何帮助:)
编辑:
var w = $(this).width();
var h = $(this).naturalHeight;
是测试,.width() 获取调整后图像的宽度(我不想要),而 naturalHeight 不起作用。
使用
elem[0].naturalWidth
改为