index.html スニペット:
<img ng-src="{{ImageURL}}" my-image/>
app.js:
var app = angular.module('plunker', []);
app.controller('MyCtl', function($scope) {
$scope.ImageURL = "";
$scope.ImgWidth = 0;
$scope.setImgSrc = function(imgURL) {
$scope.ImageURL = imgURL;
};
$scope.setImgSrc('http://angularjs.org/img/AngularJS-large.png');
});
app.directive('myImage', [function() {
return function(scope, elm, attrs) {
scope.$watch(elm.width(), function(newValue, oldValue) {
scope.ImgWidth = newValue; // always returns 0!
});
};
}]);
これがプランクです。img
変更時にカスタム ディレクティブ内の要素の新しいサイズを取得するにはどうすればよいngSrc
ですか? を適切に呼び出していない気がしますscope.$watch
。