I have a simple Angular app, I'm trying to set it up so when a person clicks on an image, a loading message is shown, then when the image loads, it hides.
When I remove scope.loading = false;
, the message is shown (obviously), but when I have that line, the message is never shown, even if the image takes 5+ seconds to load.
Can anyone see what's wrong?
app.controller('ProductsCtrl', function($scope, $interval) {
$scope.images = LayerData['images'];
$scope.mainImage = '/open/img/'+$scope.images[0].section+'/'+$scope.images[0].file;
$scope.loading = true;
$scope.main = function(img) {
$scope.mainImage = '/open/img/'+img.section+'/'+img.file;
}
});
app.directive('imageonload', function($timeout) {
return {
restrict: 'A',
link: function(scope, element, attrs) {
element.bind('load', function() {
scope.$apply(function() {
scope.loading = false;
});
});
scope.$watch(function () { return scope.mainImage; }, function (newValue) {
if (newValue) {
scope.loading = true;
element.attr('src', scope.mainImage);
}
});
element.attr('src', scope.mainImage);
}
};
});
The relevant HTML is just this, but if you need/want to see the rest let me know
<div class="row wrapper" ng-controller="ProductsCtrl">
<div col>
<div row>
<div col>
<h1>{$section.title}</h1>
</div>
</div>
</div>
{literal}
<div col>
<div row>
<div col lg="7" md="7">
<p ng-show="loading">Loading image...</p>
<div id="mainImage">
<img ng-src="{{mainImage}}" imageonload style="max-width:100%;" />
</div>
</div>
<div col lg="5" md="5">
{/literal}
{$sidebar}
{literal}
</div>
</div>
</div>
<div col class="imgList">
<div class="scrolls">
<div class="imageDiv">
<img src="/assets/media/img/{{img.section}}/thumb-100-{{img.file}}" ng-click="main(img)" ng-repeat="img in images" />
</div>
</div>
</div>
{/literal}
</div>
This is using a mixture of Smarty and Angular so that's what the {literals} and {$...} are for.