コンテナーよりも大きい場合でも、画像を実際のサイズで表示する必要があります。Image 変数を使用して、ロード時に次のようなサイズをキャプチャするというトリックを試しました。
HTML:
<div ng-controller="MyCtrl">
<input ng-model="imageurl" type="url" />
<button ng-click="loadimage()" type="button">Load Image</button>
<img ng-src="{{image.path}}"
style="width: {{image.width}}px; height: {{image.height}}px" />
</div>
Javascript:
.controller("MyCtrl", ["$scope", function ($scope) {
$scope.image = {
path: "",
width: 0,
height: 0
}
$scope.loadimage = function () {
var img = new Image();
img.onload = function () {
$scope.image.width = img.width;
$scope.image.height = img.height;
$scope.image.path = $scope.imageurl;
}
img.src = $scope.imageurl;
}
}]);
このスクリプトは機能しますが、画像が大きい場合にボタンを数回クリックした後にのみ機能します。
ワンクリックで機能させるにはどうすればよいですか?
これよりも画像サイズを発見する良い方法はありますか?